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")
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")
+ 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.
+ 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.
+ 1
contact me