用python做一个猜拳小游戏
时间: 2024-05-05 14:19:05 浏览: 9
好的,我可以回答这个问题!你可以使用Python的random模块来生成计算机随机选择,并使用条件语句来确定胜者。以下是实现该游戏的示例代码:
```python
import random
while True:
# 计算机的随机选择
computer = random.choice(['石头', '剪刀', '布'])
# 询问用户选择
user = input('你的选择是(石头/剪刀/布):')
# 确定胜者
if user == computer:
print('平局!')
elif (user == '石头' and computer == '剪刀') or (user == '剪刀' and computer == '布') or (user == '布' and computer == '石头'):
print('你赢了!')
else:
print('计算机赢了!')
# 询问是否继续游戏
again = input('是否再来一局?(yes/no):')
if again.lower() != 'yes':
break
```
你可以在控制台运行此代码,即可开始进行猜拳游戏。希望这能对你有所帮助!
相关问题
用python写一个猜拳小游戏
好的,以下是用 Python 写的一个猜拳小游戏,希望您喜欢!
```python
import random
print("欢迎来到猜拳游戏!")
print("请出拳:1-石头,2-剪刀,3-布")
while True:
player = int(input("请出拳:"))
if player == 1:
print("你出拳:石头")
elif player == 2:
print("你出拳:剪刀")
elif player == 3:
print("你出拳:布")
else:
print("输入有误,请重新输入!")
continue
computer = random.randint(1, 3)
if computer == 1:
print("电脑出拳:石头")
elif computer == 2:
print("电脑出拳:剪刀")
elif computer == 3:
print("电脑出拳:布")
if player == computer:
print("平局!")
elif (player == 1 and computer == 2) or (player == 2 and computer == 3) or (player == 3 and computer == 1):
print("恭喜你,你赢了!")
else:
print("很遗憾,电脑赢了!")
again = input("再来一局?(y/n)").lower()
if again == 'y':
continue
else:
print("游戏结束,再见!")
break
```
好了,这就是猜拳小游戏的 Python 代码,您可以直接运行并玩一玩。
用python做一个对局猜拳小游戏
好的,以下是一个简单的对局猜拳小游戏的 Python 代码:
```python
import random
def play_game():
choices = ['rock', 'paper', 'scissors']
computer_choice = random.choice(choices)
user_choice = input("Choose rock, paper, or scissors: ")
print("You chose", user_choice)
print("Computer chose", computer_choice)
if user_choice == computer_choice:
print("It's a tie!")
elif user_choice == 'rock' and computer_choice == 'scissors':
print("You win!")
elif user_choice == 'paper' and computer_choice == 'rock':
print("You win!")
elif user_choice == 'scissors' and computer_choice == 'paper':
print("You win!")
else:
print("Computer wins!")
play_game()
```
你可以在命令行中运行这个代码,然后按照提示输入你的选择,程序会随机生成计算机的选择并判断胜负。