lu_laude
lu_laude

Reputation: 91

advice needed on `Or` function

if(name == "Lu") or (age == 26):
print("Hello Lu")
elif(name == "lu" or age != 26):
print("Welcome back")

else:
print("Hello")

On my understanding about the or on the if statement, if I did not write "Lu" but write the number 26 it should print "Hello Lu" because from what I've come to understand that if the first variable is false it will then direct to the second variable and if that is true then it will print the code.

However when I run my code and write a different name aside from Lu but write the correct number 26 it doesn't print "Hello Lu" rather it prints the "Welcome back".

Upvotes: 2

Views: 61

Answers (2)

Sash Sinha
Sash Sinha

Reputation: 22370

Your code works as you think it should work for example if you took input from the user and remembered to typecast age to an int since you want to compare it with 26 not "26":

name = input("Enter your name:")
age = int(input("Enter your age:"))

if(name == "Lu") or (age == 26):
  print("Hello Lu")
elif(name == "lu" or age != 26):
  print("Welcome back")
else:
  print("Hello")

Example Usage with name something other than "Lu" or "lu" and age equaling 26:

Enter your name: Ku
Enter your age: 26
Hello Lu

As you can see it does not print "Welcome Back" but rather "Hello Lu" like you thought it should :)

Play around with other inputs here!

Upvotes: 1

Peter Morris
Peter Morris

Reputation: 23224

You should you be checking for a string or (age == '26') unless age has been converted from a string input to a number.

Upvotes: 0

Related Questions