+ 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
25 Antworten
+ 7
Correct syntax:
while True:
num = int(input()
if num == 1 or num == 2:
Or
while True:
if int(input()) in [1,2]
+ 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.
+ 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
+ 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
+ 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
+ 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
+ 1
Sammy Jamez
Any number except 0 will always give True
+ 1
Thank you!!
+ 1
You forgot to add the break at the end of the if statement
+ 1
Have started
0
Wow but why doesn’t the one in the code do the right thing??
Im so confused right now…
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.
0
Wow i get the before the or but after the or, should it return true for other numbers asides 2
0
Wow thanks a lot guys✊🏽
It was helpful…
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
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.
0
Wow you mind sharing the modified code?
I’d just rewrite it on PyCharm and run it there…
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
0
Sammy Jamez
Try it in Sololearn to see how much the input system cripples the Python learning experience.
0
Shreyaansh
Why though🤔