Why does this code act this way?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Why does this code act this way??

An input of any number higher than 2 shouldn’t print “success”. Can anyone help please!!! https://code.sololearn.com/chXur71ZovM3/?ref=app

2nd Apr 2022, 5:50 AM
James Samuel
James Samuel - avatar
24 Answers
+ 7
Correct syntax: while True: num = int(input() if num == 1 or num == 2: Or while True: if int(input()) in [1,2]
2nd Apr 2022, 5:54 AM
Simba
Simba - avatar
+ 4
Because of the operators precedence. Since `==` operator has higher precedence than or operator, it's treated as (int(input()) == 1) or 2. If input is 1, True or 2 else False or 2. So, if statement is always executed since it returns True for any input.
2nd Apr 2022, 6:23 AM
Simba
Simba - avatar
+ 3
Sammy Jamez You can understand better through this example: a = False b = True c = True print (a and b or b and c) This works like: print((a and b) or (b and c)) print(False or True) print(True) -------- a = True b = False c = True print (a and b or c) This works like: (a and b) or c ------------ a = False b = False c = True print (a or b and c) this works like: a or (b and c) ----------------- a = False b = True c = True print (a and b or c) this works like: (a and b) or c
2nd Apr 2022, 6:12 AM
A͢J
A͢J - avatar
+ 3
Sammy Jamez Either way, even if you can structure the code correctly, putting the input in a while loop will not work in Sololearn. It will work in Pydroid or a proper computer ide with a terminal output, but not with Sololearn's input system. The code below works in Pydroid, but not here in Sololearn. You just can't put the input in a while loop in Sololearn's playground. print("May we start?\n\t1. Yes\n\t2. No") while True: if input() in ("1", "2"): print("success") break else: print("You have to pick betweeen 1 or 2") print("May we start?\n\t1. Yes\n\t2. No") continue
3rd Apr 2022, 6:35 AM
Bob_Li
Bob_Li - avatar
+ 2
num = int(input()) While True: If num == 1 or num == 2: print("success") That's the syntax for the line that triggers the error
3rd Apr 2022, 4:12 PM
Nsimire Jethro Kakson
Nsimire Jethro Kakson - avatar
+ 1
Sammy Jamez int(input()) == 1 or 2 This works separately means before the 'or' is one condition and after the 'or' is another condition So int(input()) == 1 might be false on any number except 1 but after the 'or' 2 will always return true
2nd Apr 2022, 6:07 AM
A͢J
A͢J - avatar
+ 1
Sammy Jamez Any number except 0 will always give True
2nd Apr 2022, 6:13 AM
A͢J
A͢J - avatar
+ 1
Thank you!!
3rd Apr 2022, 7:05 AM
James Samuel
James Samuel - avatar
+ 1
You forgot to add the break at the end of the if statement
3rd Apr 2022, 2:20 PM
Shreyaansh
Shreyaansh - avatar
+ 1
Have started
3rd Apr 2022, 8:41 PM
KINGSLEY ACKERSON
KINGSLEY ACKERSON - avatar
0
Wow but why doesn’t the one in the code do the right thing?? Im so confused right now…
2nd Apr 2022, 5:57 AM
James Samuel
James Samuel - avatar
0
int(input()) == 1 or 2 is true if either int(input()) == 1 or 2 is true. In Python any integer other than 0 is evaluated as True. So your condition is always true.
2nd Apr 2022, 6:05 AM
Simon Sauter
Simon Sauter - avatar
0
Wow i get the before the or but after the or, should it return true for other numbers asides 2
2nd Apr 2022, 6:10 AM
James Samuel
James Samuel - avatar
0
Wow thanks a lot guys✊🏽 It was helpful…
2nd Apr 2022, 10:25 PM
James Samuel
James Samuel - avatar
0
Before coming to sololearn, i tried with pycharm… it was there the question came from. So its not bout Sololearn, its the code itself
3rd Apr 2022, 6:39 AM
James Samuel
James Samuel - avatar
0
Sammy Jamez You are right, there is an issue in your if condition. I modified your code so it works, but it really would never work in Sololearn.
3rd Apr 2022, 6:48 AM
Bob_Li
Bob_Li - avatar
0
Wow you mind sharing the modified code? I’d just rewrite it on PyCharm and run it there…
3rd Apr 2022, 6:49 AM
James Samuel
James Samuel - avatar
0
Sammy Jamez just change the if statement if input() in ("1", "2"): also maybe add a break inside the if block? look at my answer above
3rd Apr 2022, 6:51 AM
Bob_Li
Bob_Li - avatar
0
Sammy Jamez Try it in Sololearn to see how much the input system cripples the Python learning experience.
3rd Apr 2022, 7:07 AM
Bob_Li
Bob_Li - avatar
0
Shreyaansh Why though🤔
3rd Apr 2022, 3:02 PM
James Samuel
James Samuel - avatar