how to create a lock in the code

Asked

Viewed 41 times

0

if the wrong note is typed twice and it goes to the second note

a = int(input('digite a primeira nota: \n'))
if a > 10:
    a = int(input('Nota invalida digite novamente: \n'))
b = int(input('digite a segunda nota: \n'))
if b > 10:
    b = int(input('Nota invalida digite novamente: \n'))
c = int(input('digite a terceira nota: \n'))
if c > 10:
    c = int(input('Nota invalida digite novamente: \n'))
d = int(input('digite a quarta nota: \n'))
if d > 10:
    d = int(input('Nota invalida digite novamente: \n'))

media= (a + b + c + d) / 4

if media < 11:
    print('a media é : {}\n'.format(media))
else:
    print('uma ou mais notas não sao validas')

1 answer

-2

It could create an auxiliary function to keep the user in a loop while there is a successful read of an integer. Here is an example of how this auxiliary function can be implemented.

def lerInteiro():
     while True:
        try:
            a  = int(input('digite a nota: \n'))
            if a >= 0 and a <= 10:
                return a
            raise Exception
        except:
            print("Não foi possível ler o valor digitado como uma nota válida")

then could use the auxiliary function to read the values of the notes in your code, I am exemplifying here the reading of only one value, but can be done in reading all variables (a,b,c,d):

a = lerInteiro()

Explaining the code:

We put the user in a repeat loop while so that until a conversion (to whole) occurs successfully.

Additionally I put a range check of the values accepted for this case we accept only values between 0 and 10.

We use a Try block to ensure that the entered value can be converted to integer value. Otherwise we show an error message directing the user to try to solve the problem.

Browser other questions tagged

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