Python Forum
python age calculator need to find the number of years before they turn 100 not using - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: python age calculator need to find the number of years before they turn 100 not using (/thread-9175.html)



python age calculator need to find the number of years before they turn 100 not using - orangevalley - Mar-25-2018

[font=Times New Roman, Times, serif]
I am new at python and i'm trying to teach myself the best way i can. So i will make noob mistakes. I was wondering if you could tell me where my code is going wrong and how i could improve it?

I have been trying to make a simple age calculator as part of the questions i have recieved at school.

The question is:

Write a program that asks the user to enter their age (in years).

- If the user is 100 or older, tell them You've already turned 100!.
- If they are less than 0, tell them Try again after you are born!.
- If neither of these cases apply, calculate the number of years before they turn 100 and output the message You will be 100 in x years! (where x is - replaced by the number of years before they turn 100).
- You must use an if...elif...else statement for this problem.

my code


age = int(input("Enter your current age in years: "))

if (age >= 100):
   print("You've already turned 100!.") 

elif:
     for age in range( 0 > -100:)
     print("Try again after you are born!.")

else:
     x = (int(age - 100 ))
     print("You will be 100 in" x "years!" )



RE: python age calculator need to find the number of years before they turn 100 not using - Gribouillis - Mar-25-2018

elif needs a condition, just like if. I think you could follow more closely the bulleted list in your program.


RE: python age calculator need to find the number of years before they turn 100 not using - orangevalley - Mar-25-2018

so its:
elif: for age in range (o> -100:)
      print("Try again after you are born!.")



RE: python age calculator need to find the number of years before they turn 100 not using - sparkz_alot - Mar-25-2018

(Mar-25-2018, 09:53 AM)orangevalley Wrote: so its:
elif: for age in range (o> -100:)
      print("Try again after you are born!.")

Did you try running it? You typed the letter 'o' for the number '0'. Why are you using 'range' at all? Why not just use 'age <= 0' as you did when testing for an input over 100.

In your 'else:' statement, if all works correctly, 'age' will always be between 1 and 99, subtracting 100 from 'age' will always make 'x' a negative number.

Finally, in your 'else' statement, the print function will cause an error. Can you see what the problem is?


RE: python age calculator need to find the number of years before they turn 100 not using - PyMan - Mar-26-2018

(Mar-25-2018, 02:20 PM)sparkz_alot Wrote:
(Mar-25-2018, 09:53 AM)orangevalley Wrote: so its:
elif: for age in range (o> -100:)
      print("Try again after you are born!.")

Did you try running it? You typed the letter 'o' for the number '0'. Why are you using 'range' at all? Why not just use 'age <= 0' as you did when testing for an input over 100.

In your 'else:' statement, if all works correctly, 'age' will always be between 1 and 99, subtracting 100 from 'age' will always make 'x' a negative number.

Finally, in your 'else' statement, the print function will cause an error. Can you see what the problem is?
else:
    x = (int(100 - age))
    print("you will be 100 in", x ,"years!")