Search code examples
python

How do I perform a function A set number of times and countdown each time it is performed?


I've created a time calculator to tell a user their time spent in flight.

I've figured out how to create these functions and call them however I'm not sure how to repeat a function again. In this case if the user specifies multiple flights were performed I'd need to repeatedly ask the user for the information for each flight. I think this should be performed by a function that executes a number of times specified by the user or some kind of loop that adds a previous total to a new total.

But how do I write this code?

Do I have to assign a new value for every instance the user enters a new time?

How do I store previous times to add to a new time?

Here is the code:

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

I was thinking of using a for loop. E.g, for flight_number() something perform this function x amount of times.

I'm unsure how to write this in code.


Solution

  • you can add the loop in your if-clause if Mult == True. There you can try to make a list of the return values of time_calc or just add them in the for loop to your x.

    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?")
            # here for loop option 1:
            x = 0
            # check if flight_number ist a real integer
            for i in range(int(flight_number):
                take = input ("Please enter take off time:")
                land = input ("Please enter Landing time:")
                x = x + time_calc(take,land)
                print (x)
            # end of for loop
    
            # here for loop option 2:
            list_of_x = []
            for i in flight_number
                take = input ("Please enter take off time:")
                land = input ("Please enter Landing time:")
                x = time_calc(take,land)
                list_of_x.append(x)
                print (x)
            print("sum of time: " + sum(list_of_x))
            # end of for loop
        else:
            x = time_calc(take,land)
            print (x)