How to define a function that calculates the lowest speed?

2

The program must have a function that calculates the lowest speed among those that are calculated from the data provided. The user must provide as input, 9 values, 3 in 3, which correspond to speed, acceleration and elapsed time for 3 different cars. The program must calculate, from this data, the speed of each car, and print the smallest of them. How I'm trying:

def velkmh(Vi, A, T):
    V = Vi + A * T
    return V
vi1 = float(raw_input())
a1 = float(raw_input())
t1 = float(raw_input())
velkmh(vi1, a1, t1)
V1 = velkmh(vi1, a1, t1) * 3.6
vi2 = float(raw_input())
a2 = float(raw_input())
t2 = float(raw_input())
velkmh(vi2, a2, t2)
V2 = velkmh(vi2, a2, t2) * 3.6
vi3 = float(raw_input())
a3 = float(raw_input())
t3 = float(raw_input())
velkmh(vi3, a3, t3)
V3 = velkmh(vi3, a3, t3) * 3.6
    
asked by anonymous 04.04.2016 / 20:26

2 answers

1

I've made some changes. It looks like this:

def velocidadeEmMetrosPorSegundo(Vi, A, T):
    V = Vi + A * T
    return V * 3.6

vi1 = float(raw_input())
a1 = float(raw_input())
t1 = float(raw_input())
V1 = velocidadeEmMetrosPorSegundo(vi1, a1, t1)

vi2 = float(raw_input())
a2 = float(raw_input())
t2 = float(raw_input())
V2 = velocidadeEmMetrosPorSegundo(vi2, a2, t2)

vi3 = float(raw_input())
a3 = float(raw_input())
t3 = float(raw_input())
V3 = velocidadeEmMetrosPorSegundo(vi3, a3, t3)

print("A menor velocidade é: " + min(V1, V2, V3))

First, I made the function already return the value multiplied by 3.6 , since the value in Km / h is not useful at all. I changed the role name to agree. I suggest you leave your names very descriptive, even if it makes them long. You waste a little time now but win in the future.

You are calling the function twice. I removed calls that did not catch the return.

I have separated into logical blocks to make reading easier.

Finally, I used the min function, which does exactly what you need: it selects the smallest value from the past.

    
04.04.2016 / 21:00
0

I did as follows and it worked. Thanks for the help! Only now did I see your answer.

    def velkmh(Vi, A, T):
        V = Vi + A * T
        return V
    vi1 = float(raw_input())
    a1 = float(raw_input())
    t1 = float(raw_input())
    velkmh(vi1, a1, t1)
    V1 = velkmh(vi1, a1, t1) * 3.6
    vi2 = float(raw_input())
    a2 = float(raw_input())
    t2 = float(raw_input())
    velkmh(vi2, a2, t2)
    V2 = velkmh(vi2, a2, t2) * 3.6
    vi3 = float(raw_input())
    a3 = float(raw_input())
    t3 = float(raw_input())
    velkmh(vi3, a3, t3)
    V3 = velkmh(vi3, a3, t3) * 3.6
    lista = [V1, V2, V3]
    print min(lista)
    
04.04.2016 / 21:03