Python if else退出

5m1hhzi4  于 2023-01-16  发布在  Python
关注(0)|答案(5)|浏览(170)

这是一个聊天响应程序,我目前面临的问题是,我试图使它退出,如果输入的答案不匹配我的答案之一,但由于所有的答案的三个问题放在一起作为响应,所以即使是正确的问题,它随机选择输入,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()
llmtgqce

llmtgqce1#

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语句。

svdrlsy4

svdrlsy42#

代替

if
else: exit

if
else: exit

if
else: exit

执行以下操作:

if

elif:

elif:

else: exit

因为否则,如果第一个if为false,则将退出并且不检查其他的。

niknxzdl

niknxzdl3#

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()
aiqt4smr

aiqt4smr4#

您应该将提示和聊天机器人的预期答案和回复分组,例如,以(prompt, expected_answer, [some, possible, replies])的形式作为元组

feeling = ("How are you feeling right now " +str(x)+"? ", "tired",
           ['I wish I can make you feel better.','I hope school is not making you feel stressed.','You deserve the right to relax.'])
miss = ...
homesick = ...

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

prompt, ans, replies
resp = input(promp)
if resp == ans:
    print(random.choice(replies))
else:
    exit()

这也将使它更容易扩展您的聊天机器人与更多的问题/答案对,也提供不同的聊天机器人答复不同的用户答案。

5lhxktic

5lhxktic5#

不理解您的代码,但您可以执行以下操作:

if response!=("tired"):
    exit()
    return

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))

if response!=("yes"):
    exit()
    return

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))

[...]

相关问题