Python 如何修复用户输入并重新启动函数/程序
这个程序可以工作,但玩家赢了之后它不会重新启动,我如何修复这个问题?此外,如果用户没有输入“石头、布、剪刀、蜥蜴或斯波克”,游戏会自动将游戏称为平局,如何创建错误消息并重新启动程序? 代码如下: 计算机生成随机数1-5 随机输入Python 如何修复用户输入并重新启动函数/程序,python,Python,这个程序可以工作,但玩家赢了之后它不会重新启动,我如何修复这个问题?此外,如果用户没有输入“石头、布、剪刀、蜥蜴或斯波克”,游戏会自动将游戏称为平局,如何创建错误消息并重新启动程序? 代码如下: 计算机生成随机数1-5 随机输入 # Setting perameters def generateRN(): randomN = random.randint(1,5) return randomN #Setting Value of each number def getcom
# Setting perameters
def generateRN():
randomN = random.randint(1,5)
return randomN
#Setting Value of each number
def getcomputer(randomN):
if randomN == 5:
computer = "Spock"
elif randomN == 4:
computer = "lizard"
elif randomN == 3:
computer = "scissors"
elif randomN == 2:
computer = "paper"
elif randomN == 1:
computer = "rock"
return computer
# User Input for turn
def getuser():
userinput = input("Please choose rock, paper, scissors, lizard, or Spock: ")
print()
return userinput
# Message displaying the rule
def getwinner(computer, userinput):
SSpock = "Spock vaporizes rock"
PPaper = "Paper disproves Spock"
LLizard = "Lizard eats paper"
SScissors = "Scissors decapitates lizard"
Spock = "Spock smashes scissors"
Lizard = "Lizard poisons Spock"
RRock = "Rock crushes lizard"
Rock = "Rock smashes the scissors"
Scissors = "Scissors cuts paper"
Paper = "Paper covers rock"
winner = "tie"
message = ""
# All the different variances of what the computer and user can input/chose
if computer == "rock" and userinput == "scissors":
winner= "Computer"
message = Rock
elif computer == "scissors" and userinput == "rock":
winner = "You"
message = Rock
if computer == "scissors" and userinput == "paper":
winner= "Computer"
message = Scissors
elif computer == "paper" and userinput == "scissors":
winner = "You"
message = Scissors
if computer == "paper" and userinput == "rock":
winner= "Computer"
message = Paper
elif computer == "rock" and userinput == "paper":
winner = "You"
message = Paper
if computer == "rock" and userinput == "lizard":
winner= "Computer"
message = RRock
elif computer == "lizard" and userinput == "rock":
winner = "You"
message = RRock
if computer == "lizard" and userinput == "Spock":
winner= "Computer"
message = Lizard
elif computer == "Spock" and userinput == "lizard":
winner = "You"
message = Lizard
if computer == "Spock" and userinput == "scissors":
winner= "Computer"
message = Spock
elif computer == "scissors" and userinput == "Spock":
winner = "You"
message = Spock
if computer == "scissors" and userinput == "lizard":
winner= "Computer"
message = SScissors
elif computer == "lizard" and userinput == "scissors":
winner = "You"
message = SScissors
if computer == "lizard" and userinput == "paper":
winner= "Computer"
message = LLizard
elif computer == "paper" and userinput == "lizard":
winner = "You"
message = LLizard
if computer == "paper" and userinput == "Spock":
winner= "Computer"
message = PPaper
elif computer == "Spock" and userinput == "paper":
winner = "You"
message = PPaper
if computer == "Spock" and userinput == "paper":
winner= "Computer"
message = SSpock
elif computer == "paper" and userinput == "Spock":
winner = "You"
message = SSpock
return winner, message
# This is the prompt that with restart in a tie is reached
def restart():
randomN = generateRN()
computer = getcomputer(randomN)
userinput = getuser()
print("The computer chose",computer)
print()
winner, message = getwinner(computer, userinput)
if winner != "tie":
print(winner,"won",message, "" )
print()
return winner
# This is the central command that will run at start and if a tie is reached, the restart command will initate
def main():
randomN = generateRN()
computer = getcomputer(randomN)
userinput = getuser()
print("The computer chose",computer)
print()
winner, message = getwinner(computer, userinput)
if winner != "tie":
print(winner,"won",message," ")
print()
while winner == "tie":
print("Tie")
print()
winner = restart()
#Run function
main()
要重复代码,请将其包装成循环。如果没有要保持循环打开的特定条件,可以使用
while True
永久循环:
def main():
while True:
randomN = generateRN()
computer = getcomputer(randomN)
userinput = getuser()
print("The computer chose",computer)
print()
winner, message = getwinner(computer, userinput)
if winner != "tie":
print(winner,"won",message," ")
print()
while winner == "tie":
print("Tie")
print()
winner = restart()
这将导致
main
的主体“永远”循环;或者直到你杀了这个程序。您可以给循环一个条件或使用中断
在某个点离开循环。非常感谢。