Python how to check if input is a letter or character

前端 未结 4 669
暖寄归人
暖寄归人 2021-01-26 10:53

How can I check if input is a letter or character in Python?

Input should be amount of numbers user wants to check. Then program should check if input given by user be

4条回答
  •  青春惊慌失措
    2021-01-26 11:41

    You can check the type of the input in a manner like this:

    num = eval(input("Number to check:"))
    if isinstance(num, int):
        if num < 0:
            print(num+"\tFAIL. Number is minus")
        elif tribonnaci(num) == num: # it would be clean if this function also checks for the initial correct answers. 
            print(num + '\tYES')
        else:
            print(num + '\NO')
    else:
        print('FAIL, give number')
    

    and if not an int was given it is wrong so you can state that the input is wrong. You could do the same for your initial n = int(input("How many numbers do you want to check:")) call, this will fail if it cannot evaluate to an int successfully and crash your program.

提交回复
热议问题