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
Related
This question already has answers here:
When to use "while" or "for" in Python
(10 answers)
Closed 1 year ago.
There is a really differ between while and for in python? Or can I use anyone I want?
And my second question :-
persons['alic', 'sam', 'rain']
first_name = 'mike'
for first_name in persons :
#when the program runs for the first time
#what is the value of first_name?
#is it 'mike',or its value is Null??
print(first_name)
And thanks.
In general, a for in loop is useful for iteration over a known set and the number of iterations is predetermined. A while loop is generally used when the exit condition is a change of state, not a predetermined length.
Common use cases for while loops include:
Running the main loop of a program until a keyboard interrupt:
try:
while True:
break
except KeyboardInterrupt:
print("Press Ctrl-C to terminate while statement")
pass
Finding the last node in a list of references:
while(node is not None):
node = node.next()
This question is answered well in this stackoverflow
Pulling out the information:
while loop - used for looping until a condition is satisfied and when it is unsure how many times the code should be in loop
for loop - used for looping until a condition is satisfied but it is used when you know how many times the code needs to be in loop
do while loop - executes the content of the loop once before checking the condition of the while.
This question already has answers here:
How do I wait for a pressed key?
(13 answers)
Closed 2 years ago.
I was making a terminal-type program with python and I was wondering if I could make it print the next line of text after you press enter
e.g
`print('this was a triumph')
#press enter to print the next line
print('Im making a note here, huge success!')`
You could try:
input('this was a triumph')
input('Im making a note here, huge success!')
or:
print('this was a triumph')
input('Press Enter To Continue:')
print('Im making a note here, huge success!')
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 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().
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'):