python石头剪刀布代码
时间: 2023-07-02 16:22:19 浏览: 100
好的,以下是Python实现石头剪刀布游戏的代码:
```python
import random
def play():
user = input("请出拳(石头、剪刀、布):")
computer = random.choice(['石头', '剪刀', '布'])
print(f"你出拳为:{user},电脑出拳为:{computer}")
if user == computer:
return "平局"
elif is_win(user, computer):
return "你赢了!"
else:
return "电脑赢了!"
def is_win(player, opponent):
if (player == '石头' and opponent == '剪刀') or \
(player == '剪刀' and opponent == '布') or \
(player == '布' and opponent == '石头'):
return True
return False
print(play())
```
在运行代码时,程序会随机生成电脑出拳的结果,然后与用户输入的结果进行比较,从而决定胜负。
相关问题
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}")
```
python石头剪刀布游戏代码
游戏介绍:
数字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()
```
阅读全文