-2

I tried to make it only ask "do you want to continue" 3 times but it doesn't seem to work, it just kept on running. How do I fix this? It is a chat-response program which the computer askes one question and the user response.

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

for item in range(3):
    choice()

This is the code I have written for it. but it does not work.

 import random
    
    name = input("What is your name? ")
    
    def restart():
        restart=input('do you want to continue? ')
        if restart=='yes':
            choice()
        else:
            print("ok, see you later!")
            exit()
            
    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))
            restart()
        else:
            print("Sorry, I don't understand what you mean by "+response+".")
            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))
            restart()
        else:
            print("Sorry, I don't understand what you mean by "+response+".")
            exit()
    
    def miss():
         response = input("Who do you miss?")
         if response == "my mom":
             print("Mom will be in town soon")
             restart()
         else:
            print("Sorry, I don't understand what you mean by "+response+".")
            exit()
    
    def choice():
        prompts = [feeling, homesick, miss]
        random.choice(prompts)()
    
    for item in range(3):
        choice()
cigien
  • 57,834
  • 11
  • 73
  • 112
  • Whenever you execute restart() method from feeling, homesick or miss, you always execute choice() method, so it will be asking you again again until you will answer something else that "tired" for feeling, "yes" for homesick or "my mom" for miss. try to figure out better flow for mechanism of asking those questions. Maybe try to add some counter, that will terminate script after 3rd question "do you want to continue" – darvark Jan 12 '18 at 06:41

1 Answers1

1

The comment from darvark is correct. If you want to keep the rest of your code the same, then I would just modify the restart function to look something like this:

import sys

def restart():
    if input('do you want to continue? ') != 'yes':
        sys.exit()

This way, if the user responds with anything other than 'yes', the program will quit; however, if they respond with 'yes', then the call to restart will simply do nothing, and your loop should advance to the next iteration.

One more note: It is not recommended to call the exit function within a program, since it is just a helper function to be used when you're running the Python interpreter. Within a program, you should import the sys module and call sys.exit. Source: Difference between exit() and sys.exit() in Python

Collin R
  • 311
  • 2
  • 8