-4

I'm currently working on a life calculator that i have programmed in python. I need ideas of what to add to it and examples on how to add it and also how do i add a end control so i can just input end and the program stops. I'm trying to make this better because i plan to take it to a technology fair im going to. Here is my code.

print("The Life Calculator")


name = input("What is you're name? ") 
age = int(input("age:  "))


months = age * 12                 #This equals to how many months you have been alive.


days = age * 365                  #This equals to how many days you have been alive.


hours = age * 8765.81             #This equals to how many hours you have been alive.


minutes = age * 31556926          #This equals to how many minutes you have been alive.


seconds = age * 3.156e+7          #This equals to how many seconds you have been alive.


miliseconds = age * 3.15569e10    #This equals to how many miliseconds you have been alive.


microseconds = age * 3.156e+13    #This equals to how many microseconds you have been alive.


nanoseconds = age * 3.156e+16     #This equals to how many nanoseconds you have been alive.





print("This is how many months you have been alive.")               
print (months)        #This shows how many months you have been alive.

print("This is how many days you have been alive.")
print (days)         #This shows how many months you have been alive.

print("This is how many hours you have been alive.")
print (hours)         #This shows how many hours you have been alive.

print("This is how many minutes you have been alive.")
print (minutes)       #This shows how many minutes you have been alive.

print("This is how many seconds you have been alive.")
print (seconds)       #This shows how many seconds you have been alive.

print("This is how many miliseconds you have been alive.")
print (miliseconds)   #This shows how many miliseconds you have been alive.

print("This is how many microseconds you have been alive.")
print (microseconds)  #This shows how many microseconds you have been alive.

print("This is how many nanoseconds you have been alive.")
print (nanoseconds)   #This shows how many nanoseconds you have been alive.

lastline = ("this is how long you have been alive, so what are you going to do with the rest of your life?")

print (name)

print (lastline)
Sarath Subramanian
  • 20,027
  • 11
  • 82
  • 86
Austin James
  • 1
  • 1
  • 2
  • Austin, you should fix your indent. Also it's just a simple math calculating program. What is the desired thing? –  Jan 17 '15 at 02:43
  • It really doesn't make sense to take the user's age in years as an integer and then claim to report an exact number of minutes they've been alive. – xnx Jan 17 '15 at 02:59
  • This code works fine there isn't anything wrong with it and im just asking if maybe i should add things to the program to make it better if you get what i mean,. and i also wanted to now how i could make it to were i could in the program by typing end. I have tried several different ways to do it but none of them seem to work the way i wont them to are they just don't work period – Austin James Jan 17 '15 at 03:03
  • should i change it to like this is the estimation of how long you have been alive? – Austin James Jan 17 '15 at 03:05
  • @AustinJames What you mean about `end`? You want to stop running your program when user types `end`? –  Jan 17 '15 at 03:05

2 Answers2

2

Here is a spiffed-up version.

I took a lot of the repetitive statements and converted them to data:

from collections import namedtuple

TimeUnit = namedtuple("TimeUnit", ["name", "per_year"])

units = [
    TimeUnit("decade",            0.1 ),
    TimeUnit("month",            12.0 ),
    TimeUnit("fortnight",        26.09),
    TimeUnit("day",             365.25),
    TimeUnit("hour",           8765.81),
    TimeUnit("minute",        31556926),
    TimeUnit("second",        3.156e+7),
    TimeUnit("millisecond", 3.15569e10)
]

def get_float(prompt):
    while True:
        try:
            return float(input(prompt))
        except ValueError:
            pass

def main():
    print("The Life Calculator")

    name = input("What is your name? ") 
    years = get_float("What is your age? ")

    print("You have been alive for:")
    for unit in units:
        print("  {} {}s".format(years * unit.per_year, unit.name))
    print("what are you going to do with the rest of your life, {}?".format(name))

if __name__ == "__main__":
    main()
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
0

Your program is running once, not multiple times so actually you don't have to ask to user for end. However, if you want to run your program until user types end, you have to put that codes in a while loop.

If you want to stop running your program when user types end, just add this line to your program;

while True:

    #your codes here
    #end of your codes add this line
    ask=input("Want to end it?")
    if ask.lower()=="end":
        break

Using lower() so even user types END or eND or EnD etc. it will be ok.

  • ok the code you gave me is working in the python shell but when I try to use it in the console it only lets me enter my name and then it closes. – Austin James Jan 17 '15 at 03:24
  • @AustinJames If it's closes in console then something wrong in your codes. There must be an error message in console. –  Jan 17 '15 at 03:25
  • Ok but if there is a error thin why does it not tell me when i run it in the IDLE – Austin James Jan 17 '15 at 03:30
  • @AustinJames Because Console and IDLE not same. A thing may throw an error in console but it may be ok in IDLE. –  Jan 17 '15 at 03:32
  • Ok i didn't know that thanks for telling me. do you know of anyway i could figure out what the error is. and i have ran this in console before and it worked before but that was before i changed to 3.4.2. And thanks guys for the help, sorry for being a pain. – Austin James Jan 17 '15 at 03:36
  • Try to print screen when you open it on console. Press print-screen quickly try to catch the console screen then you can tell me what is the error. –  Jan 17 '15 at 03:40
  • it does not wait long enough it just quits but even if i can't get it to run n console is not a big deal i was just going to do it to make it look kinda better. – Austin James Jan 17 '15 at 03:52