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
  Trying to find the next prime number FirstBornAlbratross 8 4,073 Aug-14-2023, 01:16 PM
Last Post: deanhystad
  Calculate AGE in years Kessie1971 17 3,314 Apr-26-2023, 03:36 PM
Last Post: deanhystad
  Python Program to Find the Factorial of a Number elisahill 2 1,383 Nov-21-2022, 02:25 PM
Last Post: DeaD_EyE
  Tracking leap.py years for gregorian calendar (Exercism org) Drone4four 11 3,658 Oct-14-2022, 03:20 PM
Last Post: betinajessen
  Python calculator help but not using while loop with true, any flags variable ,break kirt6405 13 5,622 Jun-08-2021, 06:39 AM
Last Post: Larz60+
  Calculator: several options, changing the number gus17 4 3,183 Dec-28-2019, 11:22 PM
Last Post: ichabod801
  How to find the accuracy vs number of neighbours for KNN vokoyo 3 3,109 Apr-10-2019, 03:46 AM
Last Post: scidam
  Problem with code to calculate weekday for leap years! Event 2 2,794 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 4,120 Oct-17-2018, 03:26 AM
Last Post: stullis
  how to find a next prime number? iamyourfather 2 6,438 Oct-01-2017, 04:21 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

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