python - 如何修复用户输入并重新启动功能/程序
问题描述
这个程序可以运行,但是在玩家获胜后它不会重新启动,我该如何解决这个问题?另外,如果用户没有输入“rock, paper, scissors, lizard, or Spock”,游戏会自动将游戏称为平局,我该如何创建错误消息并重新启动程序?这是代码:
计算机生成随机数 1-5
随机导入
# 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
to 永远循环:
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
“永远”循环;或者直到你杀死程序。您可以给循环一个条件或使用 abreak
在某个点离开循环。
推荐阅读
- node.js - 如何在不同的端口中运行反应端口和后端端口?
- c# - 存储实例化预制件的问题
- visual-studio-code - VSCode 在尝试运行 Jupyter 笔记本时挂起?
- html - 我在我的 HTML 页面中插入了一个背景图像,但它没有显示
- python - 如何在此类中添加 original_text 和 reversed_text 属性?我应该如何修复它以获得理想的输出?
- python - 创建一个 Python 函数,该函数将为字符计数的元组创建一个字典,分配给元音、辅音或其他的键
- node.js - 当我的 API 服务器处理 aws SNS 订阅确认消息时出现意外结果
- asp.net-mvc - .NET Core 5.0 EF Migration 添加新的外部列
- xslt - XSLT 帮助 - 根据两个条件删除重复的 XML 节点
- python - 如何从基于两列的熊猫数据框创建新的汇总行