python石头剪刀布游戏代码
时间: 2023-10-09 22:08:11 浏览: 141
游戏介绍:
数字1代表石头
数字2代表剪刀
数字3代表布
游戏次数:
```python
import random
def game():
count = 0
while count < 5:
count += 1
guess = int(input("请输入你的选择(1-石头,2-剪刀,3-布):"))
computer = random.randint(1, 3)
if guess == computer:
print("平局!")
elif (guess == 1 and computer == 2) or (guess == 2 and computer == 3) or (guess == 3 and computer == 1):
print("你赢了!")
else:
print("你输了!")
game()
```
相关问题
python石头剪刀布游戏
好的,下面是一个简单的 Python 石头剪刀布游戏的代码实现:
```python
import random
print("欢迎来到石头剪刀布游戏!")
print("请输入你的选择(石头/剪刀/布):")
# 获取用户输入
user_choice = input()
# 随机生成电脑的选择
computer_choice = random.choice(["石头", "剪刀", "布"])
# 输出电脑的选择
print("电脑选择了:" + computer_choice)
# 判断胜负
if user_choice == computer_choice:
print("平局!")
elif user_choice == "石头" and computer_choice == "剪刀":
print("恭喜你获胜!")
elif user_choice == "剪刀" and computer_choice == "布":
print("恭喜你获胜!")
elif user_choice == "布" and computer_choice == "石头":
print("恭喜你获胜!")
else:
print("很遗憾,你输了!")
```
运行代码后,程序会提示用户输入石头、剪刀或布,然后随机生成电脑的选择,并判断胜负,最后输出结果。
python 石头剪刀布代码
下面是Python实现的石头剪刀布游戏代码:
```
import random
gestures = ['石头', '剪刀', '布']
player_gesture = input("请输入你的选择(石头、剪刀或布):")
computer_gesture = random.choice(gestures)
if player_gesture == computer_gesture:
result = "平局"
elif (player_gesture == '石头' and computer_gesture == '剪刀') or \
(player_gesture == '剪刀' and computer_gesture == '布') or \
(player_gesture == '布' and computer_gesture == '石头'):
result = "你赢了"
else:
result = "计算机赢了"
print(f"你的选择是:{player_gesture}")
print(f"计算机的选择是:{computer_gesture}")
print(f"结果:{result}")
```