This is about If Else Statement, How do I fix this? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

This is about If Else Statement, How do I fix this?

Hello, I created a code but I don't know where's the error. When I input "no" it still proceeds to the question under Yes here's the code: y_n = (input("I want you to answer two simple math equations \nand you only have 1 attempt. \nAre you ready? ")) if y_n == "yes" or "Yes" or "YES": print ("Let's continue...") first = (input("1. 18÷3-7+2*5 = ")) if int(first) == int(18 / 3 - 7 + 2 * 5) : print("Good. Next...") else: print("Wrong. Go back to preschool.") second = (input("2. 6*4÷12+72÷8-9 = ")) if int(second) == int(6 * 4 / 12 + 72 / 8 - 9): print("Perfect. You can be the President of the Ph.") else: print("Wrong.") elif y_n == "no" or "No" or "NO": print("Ok, go back to kinder.") THANK YOU SO MUCH

14th Jul 2022, 10:29 AM
Amica Feliciano
Amica Feliciano - avatar
3 Answers
+ 7
taking the hint from Ipang , we can also simplify the code a bit: insert a line of code that converts the input string to lower case directly after the input() line. then: ... if y_n == "yes":# now we need to check only against lower case answers like "yes" or like "no"
14th Jul 2022, 11:07 AM
Lothar
Lothar - avatar
+ 4
Use `in` operator to check whether <y_n> was in some predefined string instead. if y_n in "yesYesYES": # code else: # assume input was "No"
14th Jul 2022, 10:51 AM
Ipang
+ 3
Also, note that you're using 'or' in the wrong way. if y_n == "yes" or "Yes"... Tests if y_n equals "yes", then tests "Yes". A non-empty string evaluates to True, so this will always be true. If you want to compare a variable with multiple values, use: if variable in (value, value, value...): If doing more complex comparisons, write each one. For example: if x > 0 and x < 10:
14th Jul 2022, 7:06 PM
Emerson Prado
Emerson Prado - avatar