Python if else exit

Question:

This a chat-respond program and the problem I am currently facing is that I try to make it exit if the input answer does not match one of my answers, but since all the answers of the three of the questions were put together as Response so even the right for the question it randomly chose was inputted, python still thinks it is not right because it does not meet the other two and then it will exit. Please tell me how can I take a different approach on this programs.Thanks for the help1

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()
Asked By: user9196658

||

Answers:

Python provides nice flow control structure for this using elif. See the official documentation here: https://docs.python.org/3/tutorial/controlflow.html

So the idea is that all statements are evaluated as a unit.

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

Now the statements will run until one is True. If all are false, it will default to the else statement.

Answered By: pastaleg

Instead of

if
else: exit

if
else: exit

if
else: exit

do this:

if

elif:

elif:

else: exit

because otherwise, if the 1st ifis false, you will exit and not check the others.

Answered By: KYL3R

You should group the prompts and the expected answers and replies by the chatbot, e.g. as tuples in the form (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()

This will also make it easier to extend your chatbot with more question/answer pairs or also to provide different chatbot replies to different user-answers.

Answered By: tobias_k

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()
Answered By: Rafał Łużyński

Don’t understand your code, but you can do something like:

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

[...]
Answered By: destresa
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.