How to allow only a range of numbers?

1

I'm making a student number of student registration code, so I want the user to put a number between a range of 1 to 10, MAS also want the ranges to be float (ie: 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 do it faster without putting all of them one at a time?

    
asked by anonymous 26.02.2017 / 02:02

1 answer

2

You can do 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")
    
26.02.2017 / 02:33