Python Forum
Simple code error please help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple code error please help
#1
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')
Reply
#2
What's the error?
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
It always says 'that can't be your age' no matter what i type
Reply
#4
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
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 :
Reply
#6
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}")
Reply
#7
Thank you so much!
Reply
#8
(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
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with simple code JacobSkinner 1 314 Mar-18-2024, 08:08 PM
Last Post: deanhystad
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 480 Nov-07-2023, 04:32 PM
Last Post: snippsat
  help me simple code result min and max number abrahimusmaximus 2 902 Nov-12-2022, 07:52 AM
Last Post: buran
  Simple encoding code ebolisa 3 1,439 Jun-18-2022, 10:59 AM
Last Post: deanhystad
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,790 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Simple code question about lambda and tuples JasPyt 7 3,299 Oct-04-2021, 05:18 PM
Last Post: snippsat
  My simple code don't works !! Nabi666 1 1,601 Sep-06-2021, 12:10 PM
Last Post: jefsummers
Sad SyntaxError: from simple python example file from mind-monitor code (muse 2) warmcupoftea 4 2,820 Jul-16-2021, 02:51 PM
Last Post: warmcupoftea
  Plotting sum of data files using simple code Laplace12 3 3,039 Jun-16-2021, 02:06 PM
Last Post: BashBedlam
  Help with isinstance command (very simple code) Laplace12 2 2,002 Jul-30-2020, 05:26 AM
Last Post: Laplace12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020