Return in while - PYTHON loop

Asked

Viewed 56 times

-1

I’m starting to learn python now and I’m still lost in the syntax. In the following program I do not know what to do to restart the program, ie, return to the beginning if the user type "yes" or exit the program if the user type "no". Could someone explain to me how this function works in python?

"""
Escreva um programa que determine a pontuação do aluno.
A prova consta de 10 questões, cada uma com cinco alternativas (a, b, c, d, e).
O programa receberá os seguintes dados:
• o gabarito;
• as respostas do aluno, contendo o seu nome e suas respostas.
A partir daí o programa deverá comparar as respostas do aluno com a resposta do gabarito e, no final, exibir a pontuação do aluno.
O programa continua recebendo respostas de vários alunos até que o usuário informe que deseja parar de informar respostas de alunos.
"""

soma = 0
n = 0


while n < 10:
    print(f"Questão {n+1}:")
    gabarito = str(input("Digite o gabarito: "))
    nota = str(input("Digite a resposta do aluno: "))
    print("\n")
    n = n + 1
    if nota == gabarito:
        soma = soma + 1
print(f"A pontuação do aluno foi: {soma}")

resposta = str(input("Deseja continuar?\n" ))

1 answer

2


Well, come on.

While and for are repetition loops, ie they run a part of the code "n" times, the difference between them is that, while you use a condition to keep the repetition active, while in for you delimit how many times it should happen.

Example:

x = 0
while x < 11:
    print(x)
    x += 1

for y in range(11):
    print(y)

Both cases write from 0 to 10 on the screen but one uses 'while' and the other 'for'.


Return is used within 'functions' and serves to return a value that is inside the function outside of it. To declare functions in python we use def'

Example:

def soma(a, b):
    return a+b

print(soma(1, 2))
print(soma(4, 4))
print(soma(10, 1))

There are several ways to make your code run but at once, one of them ( no function/Return, would be to put inside a while that checks whether the user wants to continue. (It is not the most 'beautiful' in the world, but it works and becomes simple)

Something like this:

while (input("Deseja refazer? Sim / Não")) == "Sim":
    //Seu código vem aqui.

Browser other questions tagged

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