+ 1

Why is this not working

number = input() ## if number = 2: print("hi") I do not understand why this gives me a syntax error?

2nd May 2021, 9:56 AM
Dacleary
Dacleary - avatar
4 odpowiedzi
+ 5
1. number = int(input()) 2. number == 2
2nd May 2021, 9:57 AM
Abhishek Tandon
Abhishek Tandon - avatar
+ 1
Dacleary input() returns bydefault String value but 2 is numeric so you cannot compare number with string. To solve this problem just change type of number using int() method like this: number = int(input()) And also single equal (=) used for assignment. To compare values you need to use double equal (==) number = int(input()) if number == 2: print ("Hi")
2nd May 2021, 11:32 AM
A͢J
A͢J - avatar
0
It's not working because it wants better wages.
2nd May 2021, 3:30 PM
zxtychj
zxtychj - avatar
0
The input function returns string value so you have to convert your variable data from string to integer using int() function and the reason for syntax error is in the if statement at the place of condition you are assigning a new value to your variable which violate the if statement syntax. Example Code: num = int(input ("Enter Number ")) if num==2: print (" Hi ")
3rd May 2021, 4:52 PM
Yogesh Singh
Yogesh Singh - avatar