how do I compare a variable with a number? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

how do I compare a variable with a number?

I've started to learn python just now,and I'm trying to get 2 numbers from a user and compare it with 10 something like this : x = input('please enter an integer') y = input('please enter an integer') z = 10 if x+y < z : print('the result of your entered numbers are below 10') else: if x+y > z : print('the result of your entered numbers are over 10') else: print('the number is invalid') but when I used this code I get the error : '<' not supported between instances of 'str' and 'int'

8th Sep 2018, 6:18 AM
Mr.Zre
Mr.Zre - avatar
6 Answers
+ 7
This is telling you that one or more of your variables is actually a string datatype rather than an integer, and you can't do comparisons between strings and ints. wrap int() around the input like int(input('thing')) a similar thing will apply when concatenating strings together, if you use an int it won't work, so you must use str() to convert the result first. Be aware of the data types you are handling! :)
8th Sep 2018, 6:33 AM
Dan Walker
Dan Walker - avatar
+ 5
x =int( input('please enter an integer')) y =int( input('please enter an integer')) z = 10 The function input returns a string. If you convert it to integer, it should work.
8th Sep 2018, 6:29 AM
Oma Falk
Oma Falk - avatar
8th Sep 2018, 11:08 AM
Sri Raman Prasad
0
can you help me?
12th Sep 2018, 3:56 PM
Dr.Mahdi
Dr.Mahdi - avatar
0
Dr.Mahdi help? yes
12th Sep 2018, 4:44 PM
Sri Raman Prasad
0
can i have a better way to communicate with you. It's hard to say
12th Sep 2018, 6:16 PM
Dr.Mahdi
Dr.Mahdi - avatar