在 Python 的剪刀石头布游戏中创建 while 循环
Creating a while loop in Rock Paper Scissors game for Python
我对编程很陌生,正在寻找有关房主分配的帮助。赋值如下:
使用默认 python 函数编写一个程序,让用户可以在计算机上玩剪刀石头布游戏。该程序应按如下方式工作:
程序开始时,会生成一个1到3范围内的随机数。如果数字是 1,则计算机选择了岩石。如果数字是 2,那么计算机选择了纸,如果数字是 3,那么计算机选择了剪刀。
用户在键盘上输入"rock"、"paper"或"scissors"。
显示电脑的选择
根据以下规则选出获胜者。
当计算机或用户赢得单局时,游戏应结束,并在结果为平局时继续。非常感谢任何帮助。
from random import randint
#create a list of play options
t = ["Rock", "Paper", "Scissors"]
#assign a random play to the computer
computer = t[randint(1,3)]
#set player to False
player = False
while player == False:
#set player to True
player = input("Rock, Paper, Scissors?")
if player == computer:
print("Tie!")
elif player == "Rock":
if computer == "Paper":
print("You lose!", computer, "covers", player)
else:
print("You win!", player, "smashes", computer)
elif player == "Paper":
if computer == "Scissors":
print("You lose!", computer, "cut", player)
else:
print("You win!", player, "covers", computer)
elif player == "Scissors":
if computer == "Rock":
print("You lose...", computer, "smashes", player)
else:
print("You win!", player, "cut", computer)
else:
print("That's not a valid play. Check your spelling!")
#player was set to True, but we want it to be False so the loop continues
player = False
computer = t[randint(1,3)]
第一个问题是你选错了电脑。 Python 中的列表作为绝大多数编程语言中的数组,从 0 开始。在您的情况下,您生成的数字范围在 1 到 3 之间,即它永远不会获得 [=11= 的第一个元素] 并且当数字为 3 时将失败,因为列表以 2 结尾。
对于该部分,您可以生成一个从 0 到 2 的数字,或者使用 random
模块中的 choice()
方法。请注意,您需要先导入它。
此外,我宁愿将 while True
与一些 break
一起使用,而不是老式的状态变量。
这是您的代码的工作版本,进行了相同的重构:
from random import choice
#create a list of play options
t = ["Rock", "Paper", "Scissors"]
while True:
computer = choice(t)
player = input("Rock, Paper, Scissors?")
if player == computer:
print("Tie!")
elif player == "Rock":
if computer == "Paper":
print("You lose!", computer, "covers", player)
else:
print("You win!", player, "smashes", computer)
break
elif player == "Paper":
if computer == "Scissors":
print("You lose!", computer, "cut", player)
else:
print("You win!", player, "covers", computer)
break
elif player == "Scissors":
if computer == "Rock":
print("You lose...", computer, "smashes", player)
else:
print("You win!", player, "cut", computer)
break
else:
print("That's not a valid play. Check your spelling!")
我对编程很陌生,正在寻找有关房主分配的帮助。赋值如下:
使用默认 python 函数编写一个程序,让用户可以在计算机上玩剪刀石头布游戏。该程序应按如下方式工作:
程序开始时,会生成一个1到3范围内的随机数。如果数字是 1,则计算机选择了岩石。如果数字是 2,那么计算机选择了纸,如果数字是 3,那么计算机选择了剪刀。
用户在键盘上输入"rock"、"paper"或"scissors"。
显示电脑的选择
根据以下规则选出获胜者。
当计算机或用户赢得单局时,游戏应结束,并在结果为平局时继续。非常感谢任何帮助。
from random import randint
#create a list of play options
t = ["Rock", "Paper", "Scissors"]
#assign a random play to the computer
computer = t[randint(1,3)]
#set player to False
player = False
while player == False:
#set player to True
player = input("Rock, Paper, Scissors?")
if player == computer:
print("Tie!")
elif player == "Rock":
if computer == "Paper":
print("You lose!", computer, "covers", player)
else:
print("You win!", player, "smashes", computer)
elif player == "Paper":
if computer == "Scissors":
print("You lose!", computer, "cut", player)
else:
print("You win!", player, "covers", computer)
elif player == "Scissors":
if computer == "Rock":
print("You lose...", computer, "smashes", player)
else:
print("You win!", player, "cut", computer)
else:
print("That's not a valid play. Check your spelling!")
#player was set to True, but we want it to be False so the loop continues
player = False
computer = t[randint(1,3)]
第一个问题是你选错了电脑。 Python 中的列表作为绝大多数编程语言中的数组,从 0 开始。在您的情况下,您生成的数字范围在 1 到 3 之间,即它永远不会获得 [=11= 的第一个元素] 并且当数字为 3 时将失败,因为列表以 2 结尾。
对于该部分,您可以生成一个从 0 到 2 的数字,或者使用 random
模块中的 choice()
方法。请注意,您需要先导入它。
此外,我宁愿将 while True
与一些 break
一起使用,而不是老式的状态变量。
这是您的代码的工作版本,进行了相同的重构:
from random import choice
#create a list of play options
t = ["Rock", "Paper", "Scissors"]
while True:
computer = choice(t)
player = input("Rock, Paper, Scissors?")
if player == computer:
print("Tie!")
elif player == "Rock":
if computer == "Paper":
print("You lose!", computer, "covers", player)
else:
print("You win!", player, "smashes", computer)
break
elif player == "Paper":
if computer == "Scissors":
print("You lose!", computer, "cut", player)
else:
print("You win!", player, "covers", computer)
break
elif player == "Scissors":
if computer == "Rock":
print("You lose...", computer, "smashes", player)
else:
print("You win!", player, "cut", computer)
break
else:
print("That's not a valid play. Check your spelling!")