0

Why does this code output 0, for every possible input that I put in?

name = input("Enter your name here. ") print ("\nHello, " + name) steve = ["Steve" , "steve"] if name == steve[0] or steve[1]: print (0) else: print("nope")

14th Jun 2017, 7:22 AM
Johnny Dinh
Johnny Dinh - avatar
4 Antworten
+ 19
name = input("Enter your name here. ") print ("\nHello, " + name) steve = ["Steve" , "steve"] if name == steve[0] or name == steve[1]: print (0) else: print("nope")
14th Jun 2017, 7:23 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 3
To answer the question - python processes your if statement as follows: if (name==steve[0]) or (steve[1])... The first part may be true or false. However, steve[1] is a non-empty string, which evaluates to boolean True (an empty string would evaluate to False). So you have "something or True", which is always True.
14th Jun 2017, 7:30 AM
Bogdan Sass
Bogdan Sass - avatar
+ 1
if name in steve: instead of if name == steve[0] or steve[1]: should make it both correct and more English like, resulting in greater readability.
14th Jun 2017, 7:32 AM
Venkatesh Pitta
Venkatesh Pitta - avatar
+ 1
contact me
16th Jun 2017, 12:57 PM
eldar
eldar - avatar