+ 6

Whether no. is even or odd. What's wrong in this code?, why it's showing an error?

# A program to check if a number entered by user is odd or even num = int(input("enter your number: ")) if (num % 2 == 0): print("even") else: print("odd")

1st Jul 2024, 7:02 AM
Ashi Sharma
Ashi Sharma - avatar
15 Answers
+ 4
you have indentation error in your code # A program to check if a number entered by user is odd or even num = int(input("enter your number: ")) if (num % 2 == 0): print("even") else: print("odd")
5th Jul 2024, 11:03 AM
Chitranshi Gupta
Chitranshi Gupta - avatar
+ 3
The lines where `if` and `else` was written was indented. Those lines should be at the same indentation level with the line where input() was invoked (first line of code).
1st Jul 2024, 10:18 AM
Ipang
+ 2
Remove brackets in the if statement :). num = int(input("enter your number: ")) if num % 2 == 0: print("even") else: print("odd")
1st Jul 2024, 8:15 AM
Ruly
Ruly - avatar
+ 1
Oh okayy😅
1st Jul 2024, 10:03 AM
Ashi Sharma
Ashi Sharma - avatar
+ 1
Ipang Yeah, you are right. There is a slight indentation that Python does not like :D
1st Jul 2024, 10:43 AM
Ruly
Ruly - avatar
+ 1
TRY THIS ONE ...... You may change the int numb from 5 to somethimg else it will work int num = 5; if (num % 2 == 0){ System.out.print("even"); } else{ System.out.print("odd"); } } }
3rd Jul 2024, 6:38 AM
Aditi Chand
Aditi Chand - avatar
0
Dw, I had a same problem, because i am used to Java lol
1st Jul 2024, 10:06 AM
Ruly
Ruly - avatar
0
Yeah I tried it even by removing parentheses but it is showing error.
1st Jul 2024, 10:10 AM
Ashi Sharma
Ashi Sharma - avatar
0
To hard
2nd Jul 2024, 1:56 PM
Mia Cowling
Mia Cowling - avatar
0
''' old copy this reply into the code (remove code first) if it dont work then dont save changes. # A program to check if a number entered by user is odd or even num = int(input("enter your number: ")) if (num % 2 == 0): print("even") else: print("odd") new ''' num = int(input("enter your number: ")) if num % 2 == 0: print("even") else: print("odd") ''' explanation so basically, the code you written. num = int(input("enter your number: ")) ==> if (num % 2 == 0): print("even") ==> else: print("odd") the arrows point to the indents. updated code: num = int(input("enter your number: ")) if num % 2 == 0: print("even") else: print("odd") oh and the parentheses I forgor about those anyways copy this text and paste it into the code (remove the old one) see if it works '''
2nd Jul 2024, 9:47 PM
CyberX And CorruptedTiger
CyberX And CorruptedTiger - avatar
0
Aditi Chand that's java this is a python question idiot
4th Jul 2024, 5:14 PM
CyberX And CorruptedTiger
CyberX And CorruptedTiger - avatar
0
There is an indentation error in the if-else section
9th Jul 2024, 12:09 PM
Sonal Gaur
Sonal Gaur - avatar
0
The error in your code is due to incorrect indentation before the if statement. In Python, proper indentation is crucial. The if statement should align with the rest of the code at the same level. Here's the corrected code: # A program to check if a number entered by user is odd or even num = int(input("enter your number: ")) if (num % 2 == 0): # No indentation before if print("even") else: print("odd") In the corrected code, the if statement is aligned with the num assignment, and the else statement is aligned with the if. One of my client's website had simiilar issue. https://alightmotionproapks.com/
9th Aug 2024, 10:49 AM
Permish
Permish - avatar
0
The error in your code is due to incorrect indentation for the if statement. In Python, indentation is crucial as it defines the block of code that belongs to control structures like if, else, for, and while. Here’s the corrected version of your code: python Copy code # A program to check if a number entered by user is odd or even num = int(input("Enter your number: ")) if (num % 2 == 0): print("even") else: print("odd") I have created a page named codingsol on Honista App. You can get that app here: https://www.honistaapks.com/ and get more solutions of your coding problems directly into inbox.
22nd Aug 2024, 11:08 PM
Sibghatullah Ali
Sibghatullah Ali - avatar