How to allow only a range of numbers?

Asked

Viewed 693 times

1

I’m making a student grade number registration code, so I want the user to put a number between a range of 1 to 10, BUT I also want the breaks to be in float(ou seja: 0.1,0.2... 9.8,9.9,10.0).

nota_1 = float(input("Digite a primeira nota de " + str(aluno_1) + ": "))
mat_aluno_nota [1][0] = nota_1
while nota_1 not in (0,1,2,3,4,5,6,7,8,9,10):
    print("Digite um número entre 0 a 10")

Is there any way I can make it faster Without getting them all one at a time?

1 answer

2


You can do it like this:

while True:
    nota_1 = input("Digite a primeira nota de " + str(aluno_1) + ": ")
    try:
        nota_1 = float(nota_1)
    except Exception as err:
        print("Valor deve ser númerico")
    else:
        if 0 <= nota_1 <= 10:
            mat_aluno_nota [1][0] = nota_1
            break
        print("Valor deve estar entre 0 e 10")

Browser other questions tagged

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