首页 > 解决方案 > 如何修复用户输入并重新启动功能/程序

问题描述

这个程序可以运行,但是在玩家获胜后它不会重新启动,我该如何解决这个问题?另外,如果用户没有输入“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()

标签: python

解决方案


要重复代码,请将其包装在一个循环中。如果您没有要保持循环的特定条件,则可以使用while Trueto 永远循环:

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在某个点离开循环。


推荐阅读