Make list with input in python

Asked

Viewed 3,361 times

0

Good morning, you guys.
I need help to solve a part of the college exercise. The problem is relatively simple, but I’m having difficulties.
My code is this:

# Subprogramas
def verifica_peso(pesos):
    if pesos > 10:
        print("A soma dos pesos é maior do que 10.")

# Programa Principal
a, b, c, d, e = map(float, input().split())  # Definindo o peso das notas.
pesos = (a + b + c + d + e)

verifica_peso(pesos)
qnt_candidatos = int(input())

And what I need to do now is allow the user to enter, by input(), with the name, age and 5 candidate grades, all in the same input. Example of the input:

Ana 30 8.3 4.5 9.2 4.0 6.6
João 25 2.0 3.4 8.9 7.2 4.4
Pedro 30 7.8 5.0 9.2 6.0 4.6
Maria 28 5.0 6.0 7.0 5.0 4.0
Thiago 40 6.5 4.5 7.0 4.5 4.5
Raquel 26 10.0 10.0 10.0 10.0 10.0

Then I will need to use the information of the grades to make the average and age as a tiebreaker.
I thought about using Dict(), but I don’t know exactly how it would apply. Can you help me?
Grateful for the attention!

  • What’s the problem? I don’t understand the difficulty.

  • I’m not able to generate the list, every time I try the Pycharm gives some error, or when I get the list, I have problems to average. Wanted an idea of how to do.

  • But you come in with the name and age too, right? Why don’t you catch them on input also? There are only weights there.

3 answers

0

Using the function that Jorge posted you can go through the dictionary by obtaining the values of the keys and calculating the weighted average.

medias = []
candidatos = 2
pesos= [1.0,1.0,3.0,2.5,2.5]
for i in range(candidatos)
    for j in x:
        n = (float(x['n1']) * pesos[0] + float(x['n2']) * pesos[1] + float(x['n3']) * pesos[2] + float(x['n4']) * pesos[3] + float(x['n5'])
             * pesos[4]) / 10
    medias.append(n)
print(medias)
  • thanks for the formatting, I will try to learn how you do.

0

This can help you, to receive in 1 input all variables:

x, y = input('').split(',')
print(x)
print(y)
  • But he’s already done it...

  • 2

    From what I understood one of his doubts was this: "And what I need to do now is allow the user to enter, by input(), with the name, age and 5 grades of the candidates, all in the same input. Example of the input:" and yes I realized that he did with the notes the same thing, but he may not have realized that he could have ultimalized the same thing for the other variables.

-1

# o metodo obter_dados() faz o input e retorna um Dict
# com os valores organizados caso a pessoa informe no input a seguinte
# sequencia:
#     >>> Nome Idade Nota1 Nota2 Nota3 Nota4 Nota5
#
# Exemplo:
# sé a pessoa passar Ana 30 8.3 4.5 9.2 4.0 6.6
# retorna {
#     "nome": "Ana",
#     "idade": 30,
#     "nota1": 8.3,
#     "nota2": 4.5,
#     "nota3": 9.2,
#     "nota4": 4.0,
#     "nota5": 6.6
# }
#
# Para usar o valor do metodo basta fazer
# dados = obter_dados()
# dados["nome"]  => retorna "Ana"
# dados["idade"] => retorna 30
# dados["nota1"] => retorna 8.3
# dados["nota2"] => retorna 4.5
# dados["nota3"] => retorna 9.2
# dados["nota4"] => retorna 4.0
# dados["nota5"] => retorna 6.6
#
# para calcular as notas do aluno basta fazer.
# notas = (dados["nota1"] + dados["nota2"] + dados["nota3"] + dados["nota4"])
#
# não consegui entender como deve ser o calculo da idade, mas para
# calcular a idade com as notas basta usar float(dados["idade"]) + notas
#
def obter_dados():
    data = input(">>> ").split(" ")

    return {
        "nome": data[0],
        "idade": int(data[1]),
        "nota1": float(data[2]),
        "nota2": float(data[3]),
        "nota3": float(data[4]),
        "nota4": float(data[5])
    }
  • This function was very useful to me, but how can I use it in a loop to get the data not only of one, but of several students?

Browser other questions tagged

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