why does this if-statement not work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why does this if-statement not work?

x = input("value of x") if x > 60: print ("uw bagage is te zwaar") elif x < 60: print ("u mag door lopen")

31st Jul 2016, 5:55 PM
Yunus Kaya
Yunus Kaya - avatar
6 Answers
+ 6
Because the input function always returns a string, so your x variable is a string and you are trying to do math with it. You must use int() to convert it. wrap your input function inside an int() function. Like this: x = int(input("value of x")) And leave the rest of your code unchanged and test it. Hope it helps.
31st Jul 2016, 6:15 PM
Alireza M
Alireza M - avatar
0
that doesn't work:-\
31st Jul 2016, 6:10 PM
Yunus Kaya
Yunus Kaya - avatar
- 1
and you must modify x to be x=int(input("value of x")) and also you can use float if you want
31st Jul 2016, 6:14 PM
Ahmed Kamal
Ahmed Kamal - avatar
- 1
here you go x = input("value of x") if int(x) > 60: print ("uw bagage is te zwaar") elif int(x) < 60: print ("u mag door lopen") did you forgot x==60? because your program wont do anything if user inputs exactly 60.
2nd Aug 2016, 2:15 PM
Laura
Laura - avatar
- 1
x = int(input("value of x") that is enough to let it perfectly work
2nd Aug 2016, 2:18 PM
Yunus Kaya
Yunus Kaya - avatar
- 2
you must end if/elif statment by else replace elif by else
31st Jul 2016, 6:07 PM
Ahmed Kamal
Ahmed Kamal - avatar