+ 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")
17 Respuestas
+ 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")
+ 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")
+ 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")
+ 3
wow. thank you .
+ 3
is it possible to add a range like if I wanted to make middle aged span from 30-55
+ 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 ?
+ 2
thank you so much. awesome
+ 2
runnable code with David answers
https://code.sololearn.com/cTucCfS3n0i5/?ref=app
but I got a new bug with 2nd example
+ 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.
+ 2
SAJEB KHAN i see that in the final elif statement you only have one = when it should be two for comparing
+ 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.
+ 1
Kevin no problem!!
+ 1
Victor Hammersley What line was the bug? I tried each one (by 'commenting out' the others) and they all worked 🤔
+ 1
no i am comparing only one statememt which is for middle aged .so only one = will used here
+ 1
if use "=" u will get a syntax error at all.
"=" is not a condition operator.
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")
0
Give me some time for it i will compile this code on Idle ..ok