+ 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.
4 odpowiedzi
+ 3
x = int(input())
+ 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
+ 2
Enrico
You could
x=int(input("Enter a number"))
+ 1
Thanks a lot.
Is there any way to optimise the first two lines? This seems redundant:
X = input(‘Blablabla’)
X = int(X)