我创建了一个时间计算器来告诉用户他们在飞行中花费的时间。
我已经弄清楚如何创建这些函数并调用它们,但我不确定如何再次重复一个函数。在这种情况下,如果用户指定执行多个航班,我需要反复向用户询问每个航班的信息。我认为这应该由执行用户指定次数的函数或某种将先前总计添加到新总计的循环来执行。
但是我该如何编写这段代码呢?
我是否必须为用户输入新时间的每个实例分配一个新值?
如何存储以前的时间以添加到新时间?
这是代码:
#Function that calculates time accumulated.
def time_calc(take,land) :
#Take off and landing time is split into two pieces.
Tk =take[:2]
Tk2 = take[2:]
Ld = land[:2]
Ld2 = land[2:]
#The pieces of take off and landing time is converted into integers to perform math.
int(Tk)
int(Tk2)
int(Ld)
int(Ld2)
#Conditions are created in order to calculate remaining time
if Ld2 < Tk2:
Lda = int(Ld) - 1
Ld2 = 60
Flight_time1 = int(Lda) - int(Tk)
Flight_time2 = int(Ld2) - int(Tk2)
#Final value is returned
x = Flight_time1, "Hours", Flight_time2, "Minutes"
return x
else:
Flight_time1 = int(Ld) - int(Tk)
Flight_time2 = int(Ld2) - int(Tk2)
#Final value is returned
x = Flight_time1, "Hours", Flight_time2, "Minutes"
return x
#Initial function to receive input from the user and call on the calculator function to perform math
def flight_form():
Take_off1 = input ("Please enter take off time:")
Landing1 = input ("Please enter Landing time:")
Mult = input ("Was there multiple flights?(True or False)")
take = Take_off1
land = Landing1
bool(Mult)
if Mult == True:
flight_number = input ("How many?")
int(flight_number)
x = time_calc(take,land)
print (x)
else:
x = time_calc(take,land)
print (x)
flight_form()
我正在考虑使用 for 循环。例如,对于
flight_number()
某物执行此功能 x 次。
我不确定如何用代码编写它。
我会先询问涉及多少个航班,然后使用for循环调用函数“flight_form”
def flight_form():
Take_off = input ("Please enter take off time:")
Landing = input ("Please enter Landing time:")
x = time_calc(Take_off,Landing)
print (x)
number_of_flights = input ("How many times did you fly?")
int(flight_number)
for i in range(number_of_flights):
flight_form()