I'm writing a short program where users pick otions, I wrote a function (Yes/No) for the user to pick wether to return to home or quit program, when the user picks "No", the program is supposed to display a goodbye! message and quit but the loop seems to display the goodbye message but still prompting the user if they want to quit or not.
This is my function
def exit_home():
while True:
user = input("home or exit? (Yes/No)").lower()
if user == "yes":
main()
elif choice == "no":
print(quit)
quit()
print("Bye! ")
continue
else:
break
print("enter a valid input (Yes or No)")
And I get the result below
home or exit? (Yes/No)no
<IPython.core.autocall.ZMQExitAutocall object at 0x7fa9c096df10>
Bye!
home or exit? (Yes/No)
Also if there's a neater way of let user exit the program without printing the <IPython.core blah blah blah> I would appreciate the sugesstion.
Thanks
CodePudding user response:
There are several issues in your code:
- Remove
print(quit)
to get rid of<IPython.core.autocall.ZMQExitAutocall object at 0x7fa9c096df10>
- Use
return
to break the loop instead ofcontinue
print
afterbreak
doesn't make sense becauseprint
will never be executedbreak
doesn't make sense because it breaks the loop, but you want to get into another interation
Corrected code:
while True:
action = input("home or exit? (Yes/No) ").lower()
if action == "yes":
print("call main function...")
elif action == "no":
print("Bye! ")
break
else:
print("enter a valid input (Yes or No)")
Sample output:
home or exit? (Yes/No) yes
call main function...
home or exit? (Yes/No) maybe
enter a valid input (Yes or No)
home or exit? (Yes/No) no
Bye!
CodePudding user response:
<IPython.core.autocall.ZMQExitAutocall object at 0x7fa9c096df10>
was shown because you printed out the function.
Just this should work.
import sys
elif choice == "no":
print("Bye! ")
sys.exit()