【问题标题】:Relatively new to programming and can't figure out how to exit the magic8ball function back to the gamer function编程相对较新,不知道如何将magic8ball功能退出回gamer功能
【发布时间】:2017-09-29 14:56:58
【问题描述】:

我做了一个魔术 8 球程序,但是我不知道如何从魔术 8 球函数返回到玩家函数。 `

def gamer():
choice = int(input("What game do you want to play? \n[1]Magic 8 Ball  \n[2]Dice?\n[3]Exit \n"))
if choice == 1:
    def magic8ball():
        import random
        print("\nWelcome to the Magic 8 Ball Program. Input your question and hit enter for the 8 ball to respond")
        print("What is your question?")

        userinput = input()
        choices = ["It is certain", "It is decidedly so", "Without a doubt", "Yes definitely", "You may rely on it", "As I see it, yes", 
        "Most likely", "Outlook good", "Yes", 'Signs point to yes', "Reply hazy try again", "Ask again later", "Better not tell you now",
        "Cannot predict now", "Concentrate and ask again", "Dont count on it", "My reply is no", "My sources say no", "Outlook not good", 'Very doubful']
        if userinput != "exit" or "EXIT" or "Exit":
            print(random.choice(choices))
        if userinput == "exit" or "Exit" or "EXIT":
            #Where I need help
    magic8ball()
    while True:
        magic8ball()
        continue`

【问题讨论】:

标签: python python-3.x


【解决方案1】:

你必须向调用上下文返回一些信息,这样循环才能被打破。你可以这样做:

def magic8ball():
    # ....
    if userinput.lower() == "exit":
        return True
while True:
    if magic8ball():
        break

【讨论】:

    【解决方案2】:

    我不会向调用者发出信号,而是让magic8ball() 实现它自己的循环,完成后返回调用者的循环:

    import random
    
    ANSWERS = [
        "It is certain", "It is decidedly so", "Without a doubt", "Yes definitely",
        "You may rely on it", "As I see it, yes", "Most likely", "Outlook good",
        "Yes", "Signs point to yes", "Reply hazy try again", "Ask again later",
        "Better not tell you now", "Cannot predict now", "Concentrate and ask again",
        "Don't count on it", "My reply is no", "My sources say no", "Outlook not good", 'Very doubtful'
    ]
    
    def magic8ball():
    
        print("\nWelcome to the Magic 8 Ball Program.")
        print("Type your question and hit enter for the Magic 8 ball to respond.")
    
        while True:
            print("What is your question?")
    
            userinput = input("> ")
    
            if userinput.lower() == "exit":
                return
    
            print(random.choice(ANSWERS))
    
    def gamer():
        while True:
            print("What game do you want to play?\n[1]Magic 8 Ball\n[2]Dice\n[3]Exit")
    
            choice = int(input("> "))
    
            if choice == 1:
                magic8ball()
            elif choice == 2:
                pass
            elif choice == 3:
                return
    
    gamer()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-05
      • 1970-01-01
      • 1970-01-01
      • 2021-01-30
      • 2013-07-13
      • 2023-01-11
      • 1970-01-01
      • 2011-03-20
      相关资源
      最近更新 更多