0

I am having issue with running this code. When I try to run it, it says it won't work because age is a string. How do I convert the string to an integer? I have also tried to do 18 - int(age) and that won't work either.

    age = input ("How old are you? (): ")
    if int(age) > 18 :
        print("You're old enough to drink") 
    else:
        print("You're not old enough to drink. Wait", + 18 - age, "more years")
4
  • Can you share with us the output this code produces and the output you expected it to produces. Commented Nov 25, 2020 at 1:10
  • Where you do if int(age) > 18, in your own words: a) what does the int(age) part mean? b) do you expect this to modify age? Why or why not? Commented Nov 25, 2020 at 1:16
  • "I have also tried to do 18 - int(age) and that won't work either." What do you mean, "won't work"? How exactly did you "try" this; what happened when you tried; and how is that different from what is supposed to happen? Commented Nov 25, 2020 at 1:17
  • may be you are entering strings like "19.0" ....you can't directly convert str to int if str has a float in str format .... you can use float for this... float(age) Commented Nov 25, 2020 at 2:28

5 Answers 5

1
age = input("How old are you? (): "))
try:
    age = int(age)
    if age > 18 :
        print("You're old enough to drink.") 
    else:
        print(f"You're not old enough to drink. Wait {18-age} more years.")
except:
    print("You did not enter a valid age.")
Sign up to request clarification or add additional context in comments.

Comments

0

Note that input("How old are you? (): ") is int(input("How old are you? (): "))

age = int(input("How old are you? (): "))
if int(age) > 18 :
    print("You're old enough to drink") 
else:
    print("You're not old enough to drink. Wait {} more years".format(18-age))

Comments

0

You can add try.. except..

Like this:

age = input("How old are you? (): "))
try:
    age = int(age)
    if age > 18 :
        print("You're old enough to drink.") 
    else:
        print(f"You're not old enough to drink. Wait {18-age} more years.")
except ValueError:
    print("Not a valid age. Please enter again")

and by the way, you can use f' strings for string format.

or use .format :

print("You're not old enough to drink. Wait {0} more years.".format(18-age))

Comments

0
while True:
    age = input ("How old are you?  ")
#Check if the input is a positive integer
    if age.isdigit() >0:
        break
if int(age) > 18 :
    print("You're old enough to drink.") 
else:
    print("You're not old enough to drink. Wait",18 - int(age), "more years.")
#Remove the + in 18 because you already use comma after'Wait'

1 Comment

I use while loop to repeat the prompt for invalid input.
0

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:

  1. Write f at the beginning in between the first parentheses and first quotations like this print(f"Your string here")
  2. Only use one pair of quotations around the entire thing
  3. Get rid of the , and + used to join multiple strings
  4. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.