The reason why its giving you the error is because you only converted age to an int in the first if statement here:
if int(age) > 18 :
However, you are also performing a calculation here as well
print("You're not old enough to drink. Wait", + 18 - age, "more years")
in the 18 - age part. However, since you only converted to an int above, it is interpreting age as a string in the else block. In general if you want to perform multiple calculations like comparing to other values or arithmetic, you should just convert the variable (in this case age) to an int as soon as you receive the input. To do this, you simply add the int() function in your first line like this:
age = int(input ("How old are you? (): "))
However, now that your age variable is an int it will calculate correctly, but it won't print because you need to convert it back to a str after the calculation like this:
str(18 - age)
However, you can avoid this hassle by just using an f-string which is more standard and easier to read. To do this adjust your print statement to this:
print(f"You're not old enough to drink. Wait {18 - age} more years)
The steps to turn it into an f-string are as follows:
- Write
f at the beginning in between the first parentheses and first quotations like this print(f"Your string here")
- Only use one pair of quotations around the entire thing
- Get rid of the
, and + used to join multiple strings
- For any variable like
18 - age put it around {} to indicate that it is a variable
This makes the string much cleaner and more readable while also saving you the hassle of converting back and forth between int and str.
if int(age) > 18, in your own words: a) what does theint(age)part mean? b) do you expect this to modifyage? Why or why not?