How to make the program automatically recognize input as int or str?

Asked

Viewed 49 times

0

My question is simple: I want to create a code that the user type anything and the program returns some information about what he typed. (type, characters, etc.). It is a very simple program, but the idea is to understand how I can make the input to stop being always str and be what the user typed. Example: If he typed a number, I want it to be converted into an int and if it was text, let it look like str.

# NÃO CONSEGUI RESOLVER

    a=input('Digite qualquer coisa: ')
    print(type(a))
    print('Possui',len(a),'caracteres')
    # Se o caractere digitado for uma int, eu quero que caia no if. Não sei como fazer isso pq no input, por padrão sempre é str
    if type(a)==int:
        if a%2==0:
            print("Par")
        else:
            print("Ímpar")

If I set input at the beginning of the code as int, the Len() function does not work. How can I fix this too?

  • The concept of the code is strange and perhaps wrong, and the concept of the type of data and what the content is is the same thing is wrong. But to recognize if a given is a valid number you have the code in the question in the answered, and then you can do what you want. And there are some alternatives.

  • Does that answer your question ? https://answall.com/a/406912/157404

No answers

Browser other questions tagged

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