How to read 4 numbers at once, and such numbers should be separated only by space?

Asked

Viewed 153 times

1

I made that code but error on 2°line:

In this case I did this function to solve a problem that my teacher passed, to know if a student has passed, failed, passed with honors or will take final exam. Help me please.

def AnalisarSituacao():

   nota1, nota2, nota3, nota4 = float(input()) 

   mediaP= (nota1*1+nota2*2+nota3*3+nota4*4)/10

  if mediaP >=3: 
   if mediaP <7:
    print('prova final')
  if mediaP <3:
    print('reprovado')
  if mediaP >=7:
   if mediaP <9:
    print('aprovado')
  if mediaP >=9:
    print('aprovado com louvor')

2 answers

0

To receive more than one parameter through the input you can use:

    nota1, nota2, nota3, nota4 = input().split(" ")

This way the console will wait for an entry with 4 values separated by space.

Ex 9.0 6.2 7.3 8.0

Now it is necessary to convert the type(str to float), for this case you can do directly in the calculation.

    mediaP= (float(nota1)*1+float(nota2)*2+float(nota3)*3+float(nota4)*4)/10

For the rest of the code, keep an eye out for indentation.

ps.: Maybe you can refine that decision tree a little bit.

  • failed to call input - this code will fail. input().split() is the right thing to do.

  • 1

    Truth @jsbueno. I was wrong to put here. I will edit.

0

Your code can be written like this:

def analisar_situacao(media):
    if media < 3:
        print('Reprovado')
    elif media < 7:
        print('Prova final')
    elif media < 9:
        print('Aprovado')
    else:
        print('Aprovado com louvor')


nota1, nota2, nota3, nota4 = map(float, input().split())

m = (nota1 + nota2 * 2 + nota3 * 3 + nota4 * 4) / 10

analisar_situacao(m)
  • 1

    What do you think explaining better is doing for those who are beginning to understand? Explain what makes the split, what makes the map, why the way AP tried to do the input does not work alone, etc... would be cool!

Browser other questions tagged

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