Search code examples
pythonstringif-statementinteger

I want a int input varible then cnvert it to string


So i wanted to convert an int(input('Enter an integer') to str. because i wanted to code that if the user inputed a letter the python would print out this ('Error'). The reason im doing this is i was wondering if im able to do it without the use of the .isdigit() method or the "try" keyword.

letters = 'a-z' (just imagine all the letters)

user = int(input('>'))

if user >= 52:
     blank += 1

if user in letters:
     print(errro)

Solution

  • If you don't want to use str.isdigit() or any kind of exception handling then you could do this to confirm that the user input is comprised solely of digits 0-9

    v = input("> ")
    
    if not all(c in "0123456789" for c in v):
        print("Invalid input")
    

    Note:

    This is not the "right" way. Better to call int() on the input string and handle any ValueError exception that arises