p y t hon猜拳小游戏,用if…elif语句,使用randint()函数产生电脑随机数(0、1、2)
时间: 2024-05-10 14:15:53 浏览: 74
好的,下面是一个简单的Python猜拳小游戏:
```python
import random
print("猜拳游戏开始!")
print("请出拳:0-石头,1-剪刀,2-布")
player = int(input("请出拳:"))
computer = random.randint(0, 2)
if player == computer:
print("平局,再来一局!")
elif player == 0 and computer == 1 or player == 1 and computer == 2 or player == 2 and computer == 0:
print("恭喜你,你赢了!")
else:
print("很遗憾,你输了!")
print("电脑出的是:" + str(computer))
```
运行结果:
```
猜拳游戏开始!
请出拳:0-石头,1-剪刀,2-布
请出拳:1
很遗憾,你输了!
电脑出的是:0
```
注意,上述代码中,使用了`random`库中的`randint()`函数来产生随机数,`input()`函数用于获取用户输入。`if...elif`语句用于根据用户和电脑的出拳情况判断胜负。最后,将电脑的出拳结果输出。
相关问题
p y t hon猜拳小游戏,用if…elif语句,使用randint()函数产生电脑随机数(0、1、2),定义两个变量,分别保存用户和电脑的出拳
好的,以下是代码实现:
```python
import random
options = ['石头', '剪刀', '布'] # 定义出拳选项
computer_choice = random.randint(0, 2) # 随机生成电脑出拳
user_choice = int(input('请出拳:0-石头,1-剪刀,2-布\n')) # 获取用户出拳
# 判断胜负
if user_choice == computer_choice:
print('平局')
elif user_choice == 0 and computer_choice == 1:
print('你赢了')
elif user_choice == 1 and computer_choice == 2:
print('你赢了')
elif user_choice == 2 and computer_choice == 0:
print('你赢了')
else:
print('电脑赢了')
print('你出了%s,电脑出了%s' % (options[user_choice], options[computer_choice])) # 显示出拳结果
```
运行结果如下:
```
请出拳:0-石头,1-剪刀,2-布
1
电脑赢了
你出了剪刀,电脑出了布
```
p y t hon猜拳小游戏
好的,我可以帮你写一个简单的Python猜拳小游戏。
首先,我们需要导入random模块来生成计算机的随机选择。
```python
import random
```
然后,我们可以定义一个函数来获取玩家的选择,并确保他们只输入了“rock”,“paper”或“scissors”。
```python
def get_player_choice():
while True:
player_choice = input("Please choose rock, paper, or scissors: ").lower()
if player_choice in ["rock", "paper", "scissors"]:
return player_choice
else:
print("Invalid choice. Please try again.")
```
接下来,我们可以定义一个函数来生成计算机的选择。我们将使用random模块来生成0到2之间的随机整数,并将其映射到“rock”,“paper”或“scissors”。
```python
def get_computer_choice():
choices = ["rock", "paper", "scissors"]
computer_choice = random.randint(0, 2)
return choices[computer_choice]
```
然后,我们可以定义一个函数来比较玩家和计算机的选择,并根据结果返回胜利、失败或平局。
```python
def get_game_result(player_choice, computer_choice):
if player_choice == computer_choice:
return "It's a tie!"
elif player_choice == "rock" and computer_choice == "scissors":
return "You win!"
elif player_choice == "paper" and computer_choice == "rock":
return "You win!"
elif player_choice == "scissors" and computer_choice == "paper":
return "You win!"
else:
return "You lose!"
```
最后,我们可以定义主函数来运行游戏。在这个函数中,我们会提示玩家进行选择并显示计算机的选择和游戏结果。
```python
def play_game():
print("Let's play Rock, Paper, Scissors!")
player_choice = get_player_choice()
computer_choice = get_computer_choice()
print(f"The computer chose {computer_choice}.")
result = get_game_result(player_choice, computer_choice)
print(result)
```
现在我们可以调用play_game()函数来运行游戏了!
完整代码如下:
```python
import random
def get_player_choice():
while True:
player_choice = input("Please choose rock, paper, or scissors: ").lower()
if player_choice in ["rock", "paper", "scissors"]:
return player_choice
else:
print("Invalid choice. Please try again.")
def get_computer_choice():
choices = ["rock", "paper", "scissors"]
computer_choice = random.randint(0, 2)
return choices[computer_choice]
def get_game_result(player_choice, computer_choice):
if player_choice == computer_choice:
return "It's a tie!"
elif player_choice == "rock" and computer_choice == "scissors":
return "You win!"
elif player_choice == "paper" and computer_choice == "rock":
return "You win!"
elif player_choice == "scissors" and computer_choice == "paper":
return "You win!"
else:
return "You lose!"
def play_game():
print("Let's play Rock, Paper, Scissors!")
player_choice = get_player_choice()
computer_choice = get_computer_choice()
print(f"The computer chose {computer_choice}.")
result = get_game_result(player_choice, computer_choice)
print(result)
play_game()
```
阅读全文