Created
May 13, 2015 12:49
-
-
Save JuniorPolegato/c102a0cf5bfe40556c0f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
def perguntar(texto, tipo, igual_a=None, maior_que=None, menor_que=None): | |
texto += ' ' if texto[-1] in [':', '?'] else ': ' | |
while True: | |
try: | |
digitado = tipo(raw_input(texto)) | |
valido = (igual_a is None | |
and maior_que is None | |
and menor_que is None) | |
if not valido and igual_a is not None: | |
if isinstance(igual_a, (list, tuple, dict)): | |
valido = digitado in igual_a | |
else: | |
valido = digitado == igual_a | |
if not valido and (maior_que is not None or menor_que is not None): | |
if maior_que is not None and menor_que is not None: | |
valido = maior_que < digitado < menor_que | |
elif maior_que is not None: | |
valido = digitado > maior_que | |
else: | |
valido = digitado < menor_que | |
if valido: | |
return digitado | |
except ValueError: | |
pass | |
print ('Valor inválido!\n') | |
print (perguntar('Digite [S] ou [N]', str, igual_a=['S', 's', 'N', 'n'])) | |
print (perguntar('Digite um número inteiro entre 10 e 20 ou 5 ou 25', int, | |
igual_a=[5, 25], maior_que=9, menor_que=21)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment