Post

Python Programming
Python Programming@PythonPr·
You can find the error in this code. 🤔🤔🤔
Python Programming tweet media
English
17
12
129
16.5K
yourclouddude
yourclouddude@yourclouddude·
@PythonPr Bug is in the logic 👇 x % 2 == False works, but it’s confusing. Should be: x % 2 == 0 for clarity.
English
0
0
8
1.1K
PyBerry Tech 🐍🍓
PyBerry Tech 🐍🍓@PyBerryTech·
@PythonPr For x = 3: x % 2 = 1 1 == False → False → first if is skipped elif x % 2 → 1 → True So it print - even
English
0
0
6
998
Shalom Chimdike
Shalom Chimdike@Shalomshoes·
@PythonPr The logic was reversed. The elif statement ain't necessary. Should have been: x = 3 if x % 2 == 0: print("even") else: print("odd")
English
0
0
4
308
Khushbu Chaudhary
Khushbu Chaudhary@KhushbuCh731·
@PythonPr False is implied as 0 in python, x%2 (when x is 3)= 1, hence the condition for ‘if’ got false and it will move to elif. Elif is simply true. Hence, it will give odd for even value and vice versa.
English
0
0
2
564
Karathanasis Athanasios
Karathanasis Athanasios@k_athanasi·
@PythonPr There is no error, the code runs. The problem is bad logic and poor style. In if x % 2 == False and especially elif x % 2, the conditions rely on truthiness. x % 2 returns 0 or 1, so it works, but it is unclear and the even and odd outputs are reversed.
English
0
0
2
676
xReaverenDx
xReaverenDx@RevMathiasLust·
@PythonPr x = 3 if x % 2 == 1: print("odd") elif x % 2: print("even") else: print("none") Fixed it 😊
English
0
0
2
450
Jimmy Fikes
Jimmy Fikes@akajim·
@PythonPr 'even' will print since 3 % 2 is 1 (False would be 0). The first elif will print since x % 2 evaluates to True.
English
0
0
1
388
paul robin
paul robin@paulrobin905·
@PythonPr x % 2 == false is wrong logic, else: print(none)--% Unnecessary
English
0
0
1
10
Keziah
Keziah@Iam_Egoyibo·
@PythonPr X = 3 if X % 2 == False: print("odd") elif X % 2 == True: print("even") else: print("none")
English
0
0
1
274
Jeff
Jeff@Jeff72571425107·
@PythonPr for k in range(0,101) : if (k % 2) : print("k % 2 = ",k % 2,"\t",k," is odd") else : print("k % 2 = ",k % 2,"\t",k," is even") k % 2 = 0 0 is even k % 2 = 1 1 is odd k % 2 = 0 2 is even k % 2 = 1 3 is odd k % 2 = 0 4 is even . .
English
0
0
1
453
Jeff
Jeff@Jeff72571425107·
@PythonPr x = 3 3 mod 2 is 1 in the if statement 1 or none zero value will evaluate to true so if ( x % 2 == False ) : # value of 0 print("Even") also "none" would never executed as mod 2 will result always in a 0 or 1 "False" or "True", specific predicate - 1st vs implied on 2nd
English
0
0
1
402
Ece
Ece@eceecobn·
@PythonPr The error is comparing x % 2 with False. It should be compared with 0 to correctly check if the number is even.
English
0
0
0
48
Paylaş