mardi 23 juin 2020

How are these codes different? The second one somehow works but why doesn't the first one work?

I made this calculator code and for reasons I want to know, only the first if statement runs even if it's false. The second one works after I copied an answer I found here but I still don't understand, how are they different?

First code:

    while True:
print("Welcome to my calculator")
print("Type end")
print("Pick one of the options: ")
print("Add, Sub, Div, Mul")
user_input1 = input()
if user_input1 == "end":
    break
user_input2 = int(input("Select the first number. \n"))
user_input3 = int(input("Select the second number. \n"))
if user_input1 == ("Add") or ("add"):
    answer = user_input2 + user_input3
    print(answer)
elif user_input1 == ("Sub") or ("sub"):
    answer = user_input2 - user_input3
    print(answer)
elif user_input1 == ("Div") or ("div"):
    answer3 = user_input2 / user_input3
    print(answer3)
elif user_input1 == ("Mul") or ("mul"):
    answer4 = user_input2 * user_input3
    print(answer4)
else:
    print("rip")

Second code:

while True:
print("Welcome to my calculator")
print("Type end")
print("Pick one of the options: ")
print("Add, Sub, Div, Mul")
user_input1 = input()
if user_input1 == "end":
    break
user_input2 = int(input("Select the first number. \n"))
user_input3 = int(input("Select the second number. \n"))
if user_input1 == ("Add") or user_input1 == ("add"):
    answer = user_input2 + user_input3
    print(answer)
elif user_input1 == ("Sub") or user_input1 == ("sub"):
    answer = user_input2 - user_input3
    print(answer)
elif user_input1 == ("Div") or user_input1 == ("div"):
    answer3 = user_input2 / user_input3
    print(answer3)
elif user_input1 == ("Mul") or user_input1 == ("mul"):
    answer4 = user_input2 * user_input3
    print(answer4)
else:
    print("rip")

Thank you in advance

Aucun commentaire:

Enregistrer un commentaire