What is wrong with this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

What is wrong with this code?

I am learning and trying to write a simple code where someone inputs there age and they get a response based on their age? can someone explain how to do this properly I keep getting syntax errors. Thank You x= input("what is your age?") if x < 35, print ("you are young") if x > 35, print (" you are old") if x = 35, print (" you are middle aged")

22nd Sep 2018, 3:43 PM
Kevin
17 Answers
+ 6
Sure. x = int(input("what is your age?")) if x < 30: print("you are young") elif x > 55: print("you are old") else: print("you are middle aged") OR x = int(input("what is your age?")) if x < 30: print("you are young") elif x >= 30 and x <= 55: print("you are middle aged") else: print("you are old") OR x = int(input("what is your age?")) if x < 30: print("you are young") elif 30 <= x <= 55: print("you are middle aged") else: print("you are old")
22nd Sep 2018, 10:54 PM
David Ashton
David Ashton - avatar
+ 4
if this is exactly how you wrote it, your if statements are wrong. The correct code: #to make sure it's an integer x = int(input("what is your age?")) if x < 35: print("You are young") if x > 35: print("You are old") #You need two equal signes here #because if you use one = you are #asigning 35 to x which doesn't #which doesn't work so you need #two equal signs for #comparing if x == 35: print("You are middle aged")
22nd Sep 2018, 4:01 PM
Julian
Julian - avatar
+ 4
As Julian indicated, 'if' statements must end with a colon and the next line needs to be indented. Here's another way: x = int(input("what is your age?")) if x < 35: print("you are young") elif x > 35: print("you are old") else: print("you are middle aged")
22nd Sep 2018, 9:36 PM
David Ashton
David Ashton - avatar
+ 3
wow. thank you .
22nd Sep 2018, 9:39 PM
Kevin
+ 3
is it possible to add a range like if I wanted to make middle aged span from 30-55
22nd Sep 2018, 10:34 PM
Kevin
+ 3
Victor Hammersley I cannot understand the need for (re)posting code with others answer, as OP can easily copy paste it by its own way ^^ All the more that your linked code provide first a "# original question without sintax error" commented bugged solution wich doesn't quote initial Julian good answer author (you've corrected the comma syntax errors, but you don't correct the double equal miss nor the integer required conversion), but you said that no bugged David Ashton 2nd example give you a new one : have you only tried to run the code you've post ?
23rd Sep 2018, 4:54 AM
visph
visph - avatar
+ 2
thank you so much. awesome
22nd Sep 2018, 4:03 PM
Kevin
+ 2
runnable code with David answers https://code.sololearn.com/cTucCfS3n0i5/?ref=app but I got a new bug with 2nd example
23rd Sep 2018, 12:33 AM
Victor Hammersley
Victor Hammersley - avatar
+ 2
I tried to run the code. it doesn't work for me. Got your point. I was eager to show code that was easy to run directly and be helpful. Finally I got stuck in a new issue.
23rd Sep 2018, 2:54 PM
Victor Hammersley
Victor Hammersley - avatar
+ 2
SAJEB KHAN i see that in the final elif statement you only have one = when it should be two for comparing
24th Sep 2018, 10:23 AM
Julian
Julian - avatar
+ 2
input get a "String" and if you enter a number, this function make it to a string . if you enter 12 it's be "12". so u should use "int()" function to convert to integer. you can get user age and convert it to an integer like that. int(input("Please enter your age: ")) this statement get the age and convert it to integer and give it back. and it's better if use "elif" for another conditions and "=" it's mistake , you most use "==" for equal to conditions. like that : if x < 35: print("you are young") elif x > 35: print("you are old") elif x == 35: print ("you are middle aged") I think it's be ok after debug . good luck.
24th Sep 2018, 2:28 PM
Mehran sanea
Mehran sanea - avatar
+ 1
Kevin no problem!!
22nd Sep 2018, 4:03 PM
Julian
Julian - avatar
+ 1
Victor Hammersley What line was the bug? I tried each one (by 'commenting out' the others) and they all worked 🤔
23rd Sep 2018, 12:58 AM
David Ashton
David Ashton - avatar
+ 1
no i am comparing only one statememt which is for middle aged .so only one = will used here
24th Sep 2018, 2:46 PM
Shazeb khan
Shazeb khan - avatar
+ 1
if use "=" u will get a syntax error at all. "=" is not a condition operator.
24th Sep 2018, 2:50 PM
Mehran sanea
Mehran sanea - avatar
0
x=int(input("what is your age")) if x<35: print("you are young") elif x>35: print("you are old") elif x=35: print("you are middle aged")
24th Sep 2018, 10:19 AM
Shazeb khan
Shazeb khan - avatar
0
Give me some time for it i will compile this code on Idle ..ok
24th Sep 2018, 2:51 PM
Shazeb khan
Shazeb khan - avatar