Limit Input of Python 3 numbers

1

Using the following code I want to limit from 0 to 2000 the number that can be reported by num

import collections
num = int(input('Digite um número inteiro: '))    #Inputar número inteiro
binario = bin(num)[2:]     #Cortar o 0b
sequence = binario
collection = collections.Counter(sequence)
print('O número binário de {} é {}'.format(num,binario))
print('A maior sequência de 0 é {}'.format(len(max(binario.split('1'), 
key=len))))

It would limit the number to be entered in the num variable.

    
asked by anonymous 13.12.2018 / 15:23

1 answer

4

Just make one condition:

if 0 <= num <= 2000:
    # Faça algo

If you want to prompt the user for a new entry while the value is invalid, you will need an infinite loop:

while True:
    try:
        num = int(input())
        if not 0 <= num <= 2000:
            raise ValueError('Valor fora do intervalo permitido')
        break
    except ValueError as error:
        print(error)
    
13.12.2018 / 16:19