+ 1
What's wrong with this code
age =int(input()) #input your age if age >= 18: print (name + " is " + age + " years old") elif age = 17: print ("one more year!") elif age = 16: print ("i cant give you a pass") else: print ("nice try")
2 Answers
+ 7
1. The variable "name" is used in the print statement but is not defined .
You could resolve the issue with inserting š line at the top :
name=input() # Enter ur name
2. We use == for comparing values .The = is used for assignment and not comparision .
Replace = with == in elif statements
elif age==17:
elif age==16:
3. In the print statement you're trying to concatenate a string to an integer which is 'age' variable .
Convert 'age' to string using str(age)
like below:
print(name + "is" + str(age) + "years old")
Hope it helps š
+ 1
Ella ,
the print statement:
...
if age >= 18:
print (name + " is " + str(age) + " years old")
...
can also be done in a more simple way, with a better readability, performance, and the ability to handle various data types without using extra conversion. we can also avoid issues with additional or missing spaces in the output.
the arguments for output just needed to be separated by commas.
print(name, 'is', age, 'years old')