please help me why the error is coming
Answers
Given code:
entered_number = int(input("Enter your number: "))
remainder = entered_number%2
if remainder = 0:
print("Your number is even.")
else:
print("Your number is odd.")
Corrected code:
entered number = int(input("Enter your number: "))
remainder = entered_number%2
if remainder == 0: #corrected line
print("Your number is even.")
else:
print("Your number is odd.")
The error was at line 3, where the condition given was remainder = 0.
In Python, when you want to test for equality, the operator to be used is '==' [double equal to symbols]. Both '=' and '==' are different in Python. '=' is used for storing values while '==' is used to test for equality.
An example:
>>> n = 5
>>> if n == 5:
print("True")
else:
print("False")
In the code above, we first stored the value into the variable 'n' using '='. Since we wanted to test the equality of 'n' and '5', we used '=='.
Given Codé:-
entered_number=int(input("Enter your number : "))
remainder=entered_number%2
if remainder=0:
=0:#here is your error.
print("your number is even.")
else :
print("your number is odd")
Corrected Códe:-
entered_number=int(input("Enter your number : "))
remainder=entered_number%2
if remainder==0:
==0:#change "=" to "=="
print("your number is even.")
else :
print("your number is odd")
What was the error?
- In your given códe, line no. 3, if remainder=0 here is the error.
- Here you actually want to check if the remainder is equal to 0 or not.
- Correct syntax of equality check in if condition is as follows
if argument1==argument2:
print("equal")
else:
print("Not Equal")
- So, if you write if remainder=0, it will show syntax Error. Correct syntax is given above.