1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] How do I perform a function A set number of times and countdown each time it is...

Discussão em 'Python' iniciado por Stack, Outubro 3, 2024 às 23:12.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página