Python if else退出

2024-09-30 03:24:02 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一个聊天回复程序,我目前面临的问题是,如果输入的答案与我的一个答案不匹配,我会尝试让它退出,但由于这三个问题的所有答案都是作为响应放在一起的,所以即使是它随机选择的问题的正确答案也会被输入,python仍然认为这是不对的,因为它没有遇到其他两个,然后它将退出。请告诉我如何才能对这些程序采取不同的方法。谢谢您的帮助1

import random

x=input("What is your name? ")

def main():

    def feeling():
        return input("How are you feeling right now " +str(x)+"? ")

    def homesick():
        return input("Do you miss your home? ")

    def miss():
         return input("Who do you miss?")

    prompts = [feeling,homesick,miss]
    response = random.choice(prompts)()

    if response==("tired"):
                    Tired=['I wish I can make you feel better.','I hope school is not making you feel stressed.','You deserve the right to relax.']
                    print(random.choice(Tired))
    else:
        exit()

    if response==("yes"):
                    yes=["Don't worry, you will be home soon......",'I am protecting your family and loved ones, trust me on this.',"Your kingdoms has been waiting for a long time, they'd forgiven your mistakes"]
                    print(random.choice(yes))
    else:
        exit()

    if response==("my mom"):
                    print("Mom will be in town soon")
    else:
        exit()

    main()

main()

Tags: 答案youinputyourreturnifmainresponse
3条回答

Python使用elif为此提供了良好的流控制结构。请参阅此处的官方文档:https://docs.python.org/3/tutorial/controlflow.html

所以我们的想法是所有的语句都是作为一个单元来计算的。

if response==("tired"):
                Tired=['I wish I can make you feel better.','I hope school is not making you feel stressed.','You deserve the right to relax.']
                print(random.choice(Tired))

elif response==("yes"):
                yes=["Don't worry, you will be home soon......",'I am protecting your family and loved ones, trust me on this.',"Your kingdoms has been waiting for a long time, they'd forgiven your mistakes"]
                print(random.choice(yes))

elif response==("my mom"):
                print("Mom will be in town soon")
else:
    exit()

现在语句将一直运行,直到一个是True。如果都是false,它将默认为else语句。


import random

name = input("What is your name? ")

def main():

    def feeling():
        response = input("How are you feeling right now {name}?".format(name=name))
        if response == "tired":
            tired = ['I wish I can make you feel better.','I hope school is not making you feel stressed.','You deserve the right to relax.']
            print(random.choice(tired))
        else:
            exit()

    def homesick():
        response = input("Do you miss your home? ")
        if response == "yes":
            yes=["Don't worry, you will be home soon......",'I am protecting your family and loved ones, trust me on this.',"Your kingdoms has been waiting for a long time, they'd forgiven your mistakes"]
            print(random.choice(yes))
        else:
            exit()

    def miss():
         response = input("Who do you miss?")
         if response == "my mom":
             print("Mom will be in town soon")
         else:
             exit()

    prompts = [feeling, homesick, miss]
    random.choice(prompts)()

    main()

而不是

if
else: exit

if
else: exit

if
else: exit

执行以下操作:

if

elif:

elif:

else: exit

否则,如果第1个if为false,您将退出而不检查其他的。

相关问题 更多 >

    热门问题