Doubt average with Python 3.5

Asked

Viewed 21,246 times

-4

I am creating a program, using Python 3.5, to calculate average, but in my school there are some subjects with two teachers, and each one gives a different test.

The problem is that the code I created averages (Prova 1 + Prova 2)/2, which is equal to media1, (Atividade 1 + Atividade 2)/2, which is equal to average 2, and the final average is with Simulated (media1+media2+simulado)/3, and when it’s a matter I don’t put anything on P2 it’s a mistake. I was thinking, when the user enters the matter name run such code, and another name, another code, how do I do this?

My code:

nota1 = float(input("Nota1: "))

nota2 = float(input('Nota 2: '))

simulado = float(input('Nota simulado: '))

ac1 = float(input('Nota AC1: '))

ac2 = float(input('Nota AC2: '))


media1 = (nota1+nota2)/2
mediaAC = (ac1+ac2)/2
mediaf = (media1+simulado+mediaAC)/3 

print (mediaf)

if mediaf == 6:
   print("na media")


if mediaf < 6:
   print("abaixo da media")
  • 1

    Hello @gust4vo, you could put the code you have built and what you have tried in your question?

  • Okay, I’ve entered the code.

  • What error are you getting? Detail a little more...

  • It is not a mistake, I wanted to know how to do for when, for example, a variable is equal to Matematica, a certain code is executed (if mathematics has 2 proofs, the code with average for two subjects is executed), and if it is a subject with a proof, the code of that average is executed. I tried to leave P2 space blank, but the error was this: Traceback (Most recent call last): File "C: Users gusta Appdata Local Programs Python Python35-32 notateste.py", line 2, in <module> nota2 = float('Note 2: ')) Valueerror: could not Convert string to float:

  • @gust4vo, change the title with a summary of your doubt, for other users to identify your doubt and be able to help you too. Abracos!

4 answers

2

from functools import partial

notas = list(map(float,list(iter(partial(input, 'Nota: '), ''))))
ac = list(map(float,list(iter(partial(input, 'AC: '), ''))))
simulado = float(input('Nota simulado: '))

media_notas = sum(notas)/len(notas)
media_ac = sum(ac)/len(ac)
media_final = (media_notas + media_ac + simulado) / 3 


print('medias: ', media_notas, media_ac, media_final)

if media_final >= 6:
    print('na media')
else:
    print('abaixo da media')

This way you type as many notes as necessary, until the note value is empty (type 'Enter'). Example:

$ python notas.py 
Nota: 5
Nota: 10
Nota: 
AC: 5
AC: 5
AC: 
Nota simulado: 10
notas:  7.5 5.0 7.5
na media
  • Hello, the code went wrong.

  • Which error? Using which version of Python? I tried again here and it worked. Usage python2.7

  • First of all, one of the problems with your code is print, because in Python 3 needs parentheses: print('abaixo da media')

  • 1

    @When I answered the question, the python version was not written. The title, text and TAG were added in the editions. I edited the answer to run in python 3.5

1

Oops! Dude, I’m a beginner but I tried to implement a solution for you:

qtd_notas = int(input("Digite a quantidade de notas: "))
notas = 0
for i in range(0, qtd_notas):
    notas += input("Digite a nota " + str(i + 1) + ": ")

media = notas / qtd_notas

simulado = input("Nota Simulado: ")

acs = 0

for i in range(0, qtd_notas):
    acs += input("Notas AC" + str(i + 1) + ": ")

media_ac = acs/qtd_notas

media_final = (media + simulado + media_ac) / 3

print (media_final)

if media_final == 6:
    print ("na media")
if media_final < 6:
    print ("abaixo da media")

What I did was put in to, at the beginning, the user type the amount of notes that will compose the averages. Ignore the variables.

I hope I’ve helped.

  • Hello, I could not execute the code, I gave error: Traceback (Most recent call last): File "C:/Users/gusta/Appdata/Local/Programs/Python/Python35-32/teste.py", line 3, in <module> for i in range(0, qtd_notes): Typeerror: 'str' Object cannot be Interpreted as an integer

  • Oops! I edited the code, added a convert to int in qtd_notes. I hope now it gives! rsrs

0

Another way to resolve the issue is as follows:

notas = list(map(float, input('Digite as notas das provas: ').split()))
ac = list(map(float, input('Digite as notas das atividades: ').split()))
simulado = float(input('Digite a nota do simulado: '))

media_provas = (sum(notas) / len(notas))
media_ac = (sum(ac) / len(ac))
media_final = ((media_provas + media_ac + simulado) / 3)

print(f'\033[32mA Média Final é: {media_final:.1f}')

if media_final >= 6:
    print('Na média!')
else:
    print('Abaixo da média')

When we run the algorithm we receive the following message: Digite a nota das provas:. Right now we must type all the notes of evidence, in the same line, separated for a single space and press enter. Then we received the following message Digite as notas das atividades:. Right now we must type all the notes of the activities, in the same line, separated for a single space and press enter. Then we received the following message: Digite a nota do simulado:. At this point we must type the simulated note and press enter.

After you have inserted all the notes the algorithm will perform the calculation of the averages, displaying only the média final and the situação student’s.

-6

Here’s what I did, all right.

     pn=float(input('Digite sua primeira nota: '))

     sn=float(input('Digite sua segunda nota: '))

     m=(pn+sn)/2

     if m<6.00:
         print('Você não passou com {} de média'.format(m))
     if m>=6.00:
         print('Você passou com {} de média'.format(m))

Browser other questions tagged

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