How to define a function that calculates the lowest speed?

Asked

Viewed 425 times

2

The program must have a function that calculates the lowest speed among those calculated from the data provided. The user must provide as input, 9 values, of 3 in 3, which correspond to speed, acceleration and elapsed time for 3 different cars. The program must calculate, from these data, the speed of each car, and print the smallest of them. As I am 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
  • Its function velkmh should not return something? Moreover, what is 3.6?

  • 3.6 is the constant for turning m/s into km/h, I forgot to mention this. I tried to apply the function to each set of given values, but I think I did it wrong. I imagined that the calculated values would be stored by the program so that they could be used later.

  • I’ll edit the question.

  • I suggest you change V = .... for return ..... Also, if the speed in Km/h is not useful, make your function return the speed already in m/s (ie, move the * 3.6 into the function). I believe this will help.

  • I made some changes to the code. Now I just need the program to analyze each value, choose the smallest and print it out. I will return with the answer if I find.

3 answers

2


I’ve made some changes. It’s 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 for anything. I changed the name of the function to agree. I suggest you leave your names very descriptive, even if it makes them long. You waste a little time now but you win in the future.

You are calling the function twice. I have removed the calls that do not capture the return.

Separated into logical blocks for easy reading.

Finally, I used the function min, that does exactly what you need: selects the lowest value from the past.

1

I did it this way and it worked. Thanks for the help! Only now have I seen 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)

0

To resolve this issue we can use the following script:

def velocidade_media(ve, ac, te):
    velocidades_kmh = list()
    for v, a, t in zip(ve, ac, te):
        velo = (v + (a * t))
        velocidades_kmh.append(velo)
    menor_kmh = min(velocidades_kmh)
    menor_ms = (menor_kmh * 3.6)
    return menor_kmh, menor_ms


n = int(input('Quantos veículos serão estudados? '))
vel = list()
ace = list()
tem = list()
for c in range(1, n + 1):
    vel.append(float(input(f'Digite a velocidade inicial do {c}º carro: ')))
    ace.append(float(input(f'Digite a aceleração do {c}º carro: ')))
    tem.append(float(input(f'Digite o temp de percurso do {c}º carro, em horas: ')))

menor_velo_km, menor_velo_ms = velocidade_media(vel, ace, tem)

print(f'\033[32mA menor velocidade em "Km/h" é: {menor_velo_km:.2f} Km/h')
print(f'A menor velocidade em "m/s" é: {menor_velo_ms:.2f} m/s')

Note that when we execute this script, we must inform the amount of vehicles that will participate in the study (analysis). Then, we must insert velocidade inicial (Km/h), aceleração (km/h 2) and the tempo en route (h).

From that time the captured values will be stored in the lists vel, ace and tem respectively lists of speeds, accelerations and times.

Later these lists are sent as parameters to the function velocidade_media. Once there, these lists will be simultaneously traversed by the loop of repetition for with the help of function zip, calculating for each nth interaction the value of its velocidade final in Km/h and then stored their values in the list velocidades_kmh. Subsequently the lowest speed is calculated in Km/h (less_kmh) and then this speed is converted to m/s (menor_ms).

After that the function velocidade_media return two values, which are respectively, menor_kmh and menor_ms.

Soon after the print functions will show the values of the lowest speed in Km/h (less_kmh) and lowest speed in m/s (less_ms).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.