+ 2

If: else:

hi.i tried this code in python: nume=input("") if nume.endswith("ana"): print(nume + " - banana") if nume.endswith("tin"): print(nume+" - mandarin") else: print("you are lucky") why does it print all the time the "you are lucky"? if i type name "diana", it should only print "diana - banana", but it executes also the "else" print

4th Mar 2017, 8:22 PM
Staver Dan
Staver Dan - avatar
2 Réponses
+ 15
Because it first does the first 'if' if it's true it goes to the second 'if' which will be false and so it will do the 'else' as well. I suggest using 'elif' instead of that second 'if' and that 'else'.
4th Mar 2017, 8:27 PM
The Coding Sloth
The Coding Sloth - avatar
+ 2
thank you... from the first chapters and i already forgot)
4th Mar 2017, 8:39 PM
Staver Dan
Staver Dan - avatar