This question already has answers here:
Difference between multiple if's and elif's?
(9 answers)
Closed 1 year ago.
if is strong than elif in code?
or if and elif is same?
if a[number_counter] == answer:
a_count+=1
elif b[number_counter] == answer:
b_count+=1
elif c[number_counter] == answer:
c_count+=1
this code has problem than I change code to
if a[number_counter] == answer:
a_count+=1
if b[number_counter] == answer:
b_count+=1
if c[number_counter] == answer:
c_count+=1
than code not has problem.
It's different.
It basically else if but concated to elif. if you use elif you check the same condition and if one is met it exits.
If you use ifs the whole time it checks each if statement whether or not the previous one was true.
It's just normal Python Syntax.
x = 0
if x == 0:
#do something
#if True it doesn't check the rest
elif x ==1:
#do something
#if true if doesn't check the rest
elif x==2:
#do something
#if true doesn't check the rest
else:
#if none of these are True this gets exucuted
#do something
#It checks all these if statements whether or not one is true.
if x==1:
#do something
if x==2:
#something
Related
This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 14 days ago.
I am just starting to learn to code and I am experimenting with all sorts of methods. Might be a dumb question but why does this block only return the 0 value regardless of what I input?
start = input("Would you like to play? (Y/N)\n")
if start == "N" or "n":
game_state = 0
elif start == "Y" or "y":
game_state = 1
I have tried to change it in all sort of ways but I can t seem to make it work.
You need to change your if statements to:
if start == "N" or start == "n":
or to:
if start.lower() == "n":
A non-empty string will evaluate to True, so the first if statement will always run.
I am practicing functions. I made a short game to practice functions. I use the input() to interact.
When I enter "End" is where the confusion begins.
def endgame():
if input("Are you sure you want to exit?") == "yes" or input("Are you sure you want to exit?") == "y":
return 1
else:
return 0
The input() is triggered on both sides of the OR condition if I select anything that is not "yes".
This is not what I expected. Here is the entire code.
#function calls
def picker(v):
if v == "Game":
somegame()
elif v == "judo":
judo()
elif v == "End":
#x = endgame()
if endgame():
print("Closing")
else:
print("Restarting")
picker(input("Try to pick the secret again: "))
else:
picker(input("You must pick the secret: "))
def somegame():
print("Some game")
if input("Another game?") == "y":
picker(input("Pick another game: "))
else:
print("Game ending")
def judo():
print("Judo chop")
if input("Another game?") == "y":
picker(input("Pick another game: "))
else:
print("Game ending")
def endgame():
if input("Are you sure you want to exit?") == "yes" or input("Are you sure you want to exit?") == "y":
return 1
else:
return 0
#Start the game here
picker(input("Pick the game: "))
I can see that if it doesn't match the first OR condition, it asks for input again. I believe it should ask once and compare the input to both variables. I assume this means I would have double calls if I used other functions within a IF/OR structure in the same way?
Storing the input() in a separate variable for comparison works, but I imagine other functions that I might make that return a value having this behavior.
def endgame():
x = input("Are you sure you want to exit?")
if x == "yes" or x == "y":
return 1
else:
return 0
I know I am answering the question here, but the W3schools lesson doesnt describe this unexpected behavior.
Conditions like this in Python are evaluated from left to right, until the required condition is met or the end of the statement has been reached. Calling the same method in an or statement will result in the method being called multiple times until the condition is met, or not.
Example,
if 1 == 1 or 2 == 2 or 3 == 3 results in the rest of the condition past 1 == 1 to be totally ignored, since the whole condition has already been met. Somewhat similarly, if 1 == 2 or 2 == 3 or 3 == 3 results in the first two conditions to be evaluated. If you were to put a condition after 3 == 3, that would also be ignored assuming the previous condition (3 == 3) was True.
This sort of logic applies in conditions that incorporate and statements, though not exactly the same way depending on the circumstances of the if statement & its conditions.
This question already has answers here:
How to break out of while loop in Python?
(5 answers)
Closed 3 years ago.
I am wondering:
First, is it possible to exit a
while True loop and move onto the next piece of code? This is my
while True code that I am using:
while True:
b = input("Which row of transition metals would you like to find out the melting and boiling points of? ")
print("")
if b == "1" or b == "1st" or b == "first" or b == "First":
print("First row of transition metals:")
slg(listA, a)
again = input("Continue? ")
if again == "yes":
continue
elif b == "2" or b == "2nd" or b == "second" or b == "Second":
print("Second row of transition metals:")
slg(listB, a)
again = input("Continue? ")
if again == "yes":
continue
Is it possible so that if I had if again == no, it would move on to the next piece of code, exiting the while True loop?
Use break statement to exit out of the while loop. It works with for loop as well.
The break keyword is used to exit a loop. Note that you don't need to repeat code that asks for continuation; you can write it once after your if statement.
while True:
b = input("Which row of transition metals would you like to find out the melting and boiling points of? ")
print("")
if b == "1" or b == "1st" or b == "first" or b == "First":
print("First row of transition metals:")
slg(listA, a)
elif b == "2" or b == "2nd" or b == "second" or b == "Second":
print("Second row of transition metals:")
slg(listB, a)
else:
# Without a valid response, ask again
continue
# Only ask to continue or break after a valid response
again = input("Continue? ")
if again != "yes":
break
you can also use approach like this, where you set the conditional variable true at begining then in code when the condition to set code exit come make that variable False
Below is a example code
cond = True
while cond:
// some code
if <condition>:
continue
else:
cond = False
count =1
while True:
print("Inside while loop")
for i in "python":
if i == "h":
count = 5
break
else:
print(i)
if count == 5:
break
Please have a look at the above code.
The output for the above code is -
Inside while loop
p
y
t
The above code shows that the keyword break can be used to exit from for and while loops.
The keyword continue takes it to the to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.
From what you asked, if again == no, if this is provided you can exit from the while loop if you give something like this
if again == no:
break
But if you just give the below code,
if again == no:
continue
it will still be in the while loop.
Hope this helps.
This question already has an answer here:
Python : Exiting for loop?
(1 answer)
Closed 5 years ago.
Say I have something in this kind of format:
while True1:
if something:
do some thing
elif something else:
do something else
while True2:
if something1:
do some thing1
if something2:
do some thing2
if want to end this while True2 loop
go back to first while True1 loop
elif something else else:
do some thing else else
etc.
How can I make something within a while True loop go back to a previous while True loop that it was nested inside?
The break statement does just that.
while True: #1
if something:
do something
elif something else:
do something else
while True: #2
if something1:
do something
if something2:
do something else
if want to end this while True #2 loop:
break # will break out of the innermost loop only
elif something else else:
do some thing else else
Read more: Control Flow Tools
Use the break statement.
x = 0
while True:
x += 1
if x == 5:
print(x)
break
will output 5 and exits
Hello there I'm currently trying to get a good grasp of the if, elif, else structure in Python. I'm trying some weird combinations in python having a test program to know the output in this if, if, elif, elif, else code. However I'm getting weird results such as this
input = raw_input('Please enter the required digit: ')
intput = int(input)
if intput == 0:
print 'if1'
if intput == 1:
print 'if2'
elif intput == 0:
print 'elif1'
elif intput == 1:
print 'elif2'
else:
print 'else'
if I in put 1 it will print "if2", I thought that it will also print "elif2" and other shenanigans when I try to change the "intput == n" code. So my question is do I have to stick to the if,elif, elif, .... n * elifs, else method which seems to me working alright than working with the wacky if,if.... n * ifs, elif, elif, ...n* elifs, else.
Thanks
The elif tree is designed such that at anywhere along if one of the statement turns out to be True, the rest of the elifs will not be evaluated.
Here's a tutorial that might help you understand if else better.
this may be more clear to understand:
if input == 0:
print "if1"
switch(input):
case 1:
print "if2"
break
case 0:
print "elif1"
break
case 1:
print "elif2"
break
default:
print "else"
break
of course, the code does not work.