Python Forum
python age calculator need to find the number of years before they turn 100 not using
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python age calculator need to find the number of years before they turn 100 not using
#1
[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!" )
Reply
#2
elif needs a condition, just like if. I think you could follow more closely the bulleted list in your program.
Reply
#3
so its:
elif: for age in range (o> -100:)
      print("Try again after you are born!.")
Reply
#4
(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?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
(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!")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python calculator help but not using while loop with true, any flags variable ,break kirt6405 13 1,785 Jun-08-2021, 06:39 AM
Last Post: Larz60+
  Calculator: several options, changing the number gus17 4 1,707 Dec-28-2019, 11:22 PM
Last Post: ichabod801
  How to find the accuracy vs number of neighbours for KNN vokoyo 3 1,723 Apr-10-2019, 03:46 AM
Last Post: scidam
  Problem with code to calculate weekday for leap years! Event 2 1,412 Dec-15-2018, 05:13 PM
Last Post: Event
  i need help in fixing my scientific calculator coding : (, im using python 3.5.5 hans77 1 2,848 Oct-17-2018, 03:26 AM
Last Post: stullis
  how to find a next prime number? iamyourfather 2 4,889 Oct-01-2017, 04:21 PM
Last Post: gruntfutuk
  human years to dog years jhall710 7 8,482 May-08-2017, 05:57 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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