我的生活计算器项目

时间:2015-01-17 02:33:10

标签: python projects-and-solutions

我正在研究一个我用python编程的生活计算器。我需要添加内容的想法以及如何添加它的示例以及如何添加结束控件以便我可以输入结束并且程序停止。我正在努力做到这一点,因为我打算把它带到一个技术公平。这是我的代码。

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)

2 个答案:

答案 0 :(得分:2)

这是一个精神充沛的版本。

我接受了很多重复的陈述并将它们转换为数据:

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()

答案 1 :(得分:0)

您的程序运行一次,而不是多次,所以实际上您不必向用户询问end。但是,如果要在用户键入end之前运行程序,则必须将该代码放在while循环中。

如果您想在用户输入end时停止运行程序,只需将此行添加到您的程序中;

while True:

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

使用lower(),即使用户输入END or eND or EnD etc.,也可以。