How to make a function with Python data validation? type Try

Asked

Viewed 71 times

0

  1. When I do this code outside a function it works, now when I do with function it returns me 0.

     ```
     def validacao(x):
         try:
             x = int(input())
             band = 1
         except ValueError:
             band = 0
         while band==0:
             print("Somente números!")
             try:
                 x = int(input())
                 band = 1
             except ValueError:
                 band = 0
    
     print("1 - Inserir registro de um cliente")
     print("2 - Listar os registros dos Bebes")
     print("3 - Buscar pelo registro de um Bebe")
     op = 0
     validacao(op)
     ```
    
  • 1

    This code is very confusing and makes no sense: https://answall.com/q/456654/101 and https://answall.com/a/428367/101 and https://answall.com/a/433051/101 and https://pt.stacklowoverf.com/a/433468/101 and https://en.stackoverflow.com/q/451936/101

  • The function validacao does not have a return? And please review the indentation of your code in the question. Currently there is a while band == 0 that would generate endless "Numbers only" messages and crash your program.

  • About while, it has an input below after Try, to validate once again, it doesn’t keep generating endless messages. When I do this validation without being inside gave a function it works. Thank you

  • @Victorhugo, I don’t know if it was a mistake to copy here but it seems your code has an indentation problem. The Try and excpet should be inside the while, otherwise the code makes no sense.

No answers

Browser other questions tagged

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