Мой проект расчета жизни
В настоящее время я работаю над калькулятором жизни, который я запрограммировал на python. Мне нужны идеи о том, что добавить к нему, и примеры о том, как добавить его, а также о том, как мне добавить конечный элемент управления, чтобы я мог просто ввести end и программа остановилась. Я пытаюсь сделать это лучше, потому что я планирую принять его на технологической ярмарке, на которой я собираюсь. Вот мой код
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 ответа
Вот увеличенная версия.
Я взял много повторяющихся утверждений и преобразовал их в данные:
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()
Ваша программа выполняется один раз, а не несколько раз, поэтому на самом деле вам не нужно просить пользователя 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.
все будет хорошо.