Python Forum

Full Version: Simple code error please help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
try:
    age=int(input('please enter your age: '))
    if age> 0:
        print("You are" + age + "years old!")
    elif age> 90:
        print('you are too old!')
except:
    print('that can\'t be your age')
What's the error?
It always says 'that can't be your age' no matter what i type
You cant add number to string.
you will need to change age to a string.
Try this
try:
    age = int(input('Enter age: '))
    if age < 90:
        print(f'You are {age} years old')
    elif age >= 90:
        print('You are too old')
    else:
        print('I don\'t know your age')
except ValueError as error:
    print('There has been an error')
Output:
Enter age: 91 You are too old Enter age: 15 You are 15 years old Enter age: pp There has been an error
Gives error :(
Error:
File "C:/Users/bntayfur/Desktop/untitled8.py", line 8 try: ^ IndentationError: unexpected indent

i figured it out but what was the main reason for mine not to work? what difference does the f' inside print make as well as the except part?

Btw it says variable assigned but never used (for error) so i think it is more accurate just to say except except ValueError :
You can't add strings and ints. This is what you were attempting.

s = "a string"
i = 32
# print(s + i). Can't add strings and ints
# TypeError: unsupported operand type(s) for +: 'int' and 'str'

# you can add strings, so make one that looks like your int.
print(s + str(i))
# The f-string style does this by assembling the string representation of any object
print(f"{s}{i}")
Thank you so much!
(Jun-02-2020, 09:46 PM)bntayfur Wrote: [ -> ]Gives error :(
[error]
File "C:/Users/bntayfur/Desktop/untitled8.py", line 8
try:
^
IndentationError: unexpected indent

This happens because of your indentation, fix the indentation of your code