Created
November 24, 2017 19:19
-
-
Save echiesse/e33ad9c20a3703236bacc78deeee4f62 to your computer and use it in GitHub Desktop.
Exemplo de validação de dados de input a partir do teclado em programas de linha de comando.
This file contains 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
# Exemplo de validação de dados numéricos a partir da linha de comando. | |
# Entry Point | |
def main(): | |
while True: | |
print("Entrada inválida. Apenas numeros são aceitos") | |
strNum = input("Digite um numero: ") | |
num = asNumber(strNum) | |
if num != None: | |
break | |
print("{} eh um numero".format(num)) | |
# Converter para numero se possivel. Senão retornar None | |
def asNumber(str): | |
num = None | |
try: | |
num = float(str) | |
except: | |
pass | |
return num | |
# Executar: | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment