This question already has answers here:
How to use sys.exit() in Python
(6 answers)
Closed 4 years ago.
So I'm trying to make a little fun code to test my skills, and I'm trying to get the first if statement to continue to the next if statement, that is if they answer yes. If they answer anything other than yes, the code ends. How do I change it so the code actually ends if they answer incorrectly?
print('Would you like to begin?')
answer = input()
if answer == 'yes':
print('What is your name?')
#continue to the next portion when if statement is true
else:
print('ok')
#break the code when else statement is true
name = input()
if name == 'Test':
print('Congratulations! You won!')
else:
print('Good try. The answer was Test.')
Replace #break the code when else statement is true with import sys; sys.exit(0).
If they answer 'yes', it will simply continue to name = input().
Related
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 1 year ago.
I would like to restart my python code from the beginning after a given "yes" input from the user, but I can't understand what I'm doing wrong here:
if input("Other questions? ") == 'yes' or 'yeah':
main()
else:
pass
my code is included within the function main().
Thanks for the help!!
You would probably do it with a while loop around the whole thing you want repeated, and as Loic RW said in the comments, just break out of the while loop:
while True:
# do whatever you want
# at the end, you ask your question:
if input("Other questions? ") in ['yes','yeah']:
# this will loop around again
pass
else:
# this will break out of the while loop
break
This question already has answers here:
How do I terminate a script?
(14 answers)
Closed 3 years ago.
My break function is not working even though it is in the loop. Please, someone, explain in detail, because I'm new to Python.
q1 = input('want a question?: ')
if q1 == 'yes':
print("let's get started!, press enter")
a1 = input()
else:
print('why did you even start me ?!')
break
Here I expect break to stop the program from going any further.
break doesn't stop your program from going any further. It breaks out of a loop.
You may want sys.exit, e.g.
import sys
sys.exit()
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 5 years ago.
i am new with python and i am trying to make a survey but when i write this code things don't go well
this is the first part of my very long survey:
#a program to test your adhd
yes=1
YES=1
Yes=1
no=0
NO=0
No=0
print("please honestly answer the following questions","\n"
"with \"yes\" or \"no\" ")
a=input("1. do you have difficulty getting organized ?")#q1
if a==yes or YES or Yes or no or NO or No:
b=input("2. When given a task, you usually procrastinate rather than doing it right away")#q2
else:
print("wrong answer")
a=input("1. do you have difficulty getting organized ?")#q1
the idea of this is when the user write one of the true answers the program move to the next question.
and if he wrote other things the program print wrong answer and repeat the question.
but when tested with python shell and c.m.d it never consider the (else statement)
note that: i don't know many things in python (besides if and else statements) yet
as i am at the very beginning on learning steps.
Notice that a is a string, and you'll have to test each condition separately (don't forget the quotes!), like this:
if a == 'yes' or a == 'YES' or a == 'Yes' or a == 'no' or a == 'NO' or a == 'No':
Or a simpler alternative:
if a.lower() in ('yes', 'no'):
This question already has answers here:
How to make program go back to the top of the code instead of closing [duplicate]
(7 answers)
Closed 8 years ago.
I've written a BMI calculator in python 3.4 and at the end I'd like to ask whether the user would like to use the calculator again and if yes go back to the beginning of the code. I've got this so far. Any help is very welcome :-)
#Asks if the user would like to use the calculator again
again =input("Thank you again for using this calculator, would you like to try again? Please type y for yes or n for no-")
while(again != "n") and (again != "y"):
again =input("Please type a valid response. Would you like to try again? Please type y for yes or n for no-")
if again == "n" :
print("Thank you, bye!")
elif again == "y" :
....
Wrap the whole code into a loop:
while True:
indenting every other line by 4 characters.
Whenever you want to "restart from the beginning", use statement
continue
Whenever you want to terminate the loop and proceed after it, use
break
If you want to terminate the whole program, import sys at the start of your code (before the while True: -- no use repeating the import!-) and whenever you want to terminate the program, use
sys.exit()
You just need to call the function if the user wants to start again:
def calculate():
# do work
return play_again()
def play_again():
while True:
again = input("Thank you again for using this calculator, would you like to try again? Please type y for yes or n for no-")
if again not in {"y","n"}:
print("please enter valid input")
elif again == "n":
return "Thank you, bye!"
elif again == "y":
# call function to start the calc again
return calculate()
calculate()
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 8 years ago.
So I am working on a program that essentially asks a person if they want to hear a joke; if they say yes, then it will proceed to tell a joke; if they say no, or stop/quit the program will stop; and if they don't say yes or no, it will give them an error message. However, the program seems to just want to proceed on with the joke no matter what they input. I will produce an example of the code.
import sys
print("Want to hear a joke?")
answer = input()
if answer == "Yes" or "yes" or "y":
print("Here is a joke")
elif answer == "No" or "no" or "n":
sys.exit("Bye!")
else:
print("Please input a different response.")
This is NOT how you do a conditional
if answer == "Yes" or "yes" or "y": # this will always evaluate to true
do this instead
if answer in ["Yes" ,"yes" , "y"]:
print "Here is a joke"