+ 1

why this simple code doesn’t compile?

x = input(‘type a number’) int(x) if x > 11: print("greater than 11") if x < 11: print("smaller than 11") else: print('equal to 11') Error message : cannot compare str with int... but i converted the user’s input from str to int using int(x) Please someone tell me what I am doing wrong.

26th Jul 2018, 8:57 AM
Enrico
4 odpowiedzi
+ 3
x = int(input())
26th Jul 2018, 9:37 AM
davy hermans
davy hermans - avatar
+ 2
You need to write x=int(x) To assign the int to x. Otherwise it will convert, but not store Also make sure to write elif in your second statement. Otherwise it will print the else case for every number greater than 11. To seperate the input from the output messages, write \n at the end of your input string
26th Jul 2018, 9:08 AM
Matthias
Matthias - avatar
+ 2
Enrico You could x=int(input("Enter a number"))
26th Jul 2018, 9:37 AM
Dlite
Dlite - avatar
+ 1
Thanks a lot. Is there any way to optimise the first two lines? This seems redundant: X = input(‘Blablabla’) X = int(X)
26th Jul 2018, 9:21 AM
Enrico