I am a cat
I am a cat

Reputation: 25

How to make user inputs only accept string and not any integers. Without the use of any python modules

Numbers cannot be accepted into the input but only letters.

ans = str(input('Select an option?'))

if ans=="A":
    if len(array) < 10:
        A = list(input('\nInput string: \n'))
        while True:
            try:
                A = list(input('\nInput string: \n'))
                if A not in (1, 2, 3, 4, 5, 6, 7, 8, 9, 0):
                    raise ValueError()
                break
            except ValueError:
                print('Numbers are not accepted')
        if len(A) == 1 and str(A):
            array += A
        if len(A) >= 2:
            print('Only single digit inputs allowed')
        if len(array) == 10:
            print( "Invalid input\n")   

elif ans=="P":
    print(' '.join(array))

The input currently doesn't accept any input and is stuck in a constant loop of not accepting it, I only want letters to be accepted for the input and that it only asks the user once.

Upvotes: 1

Views: 69

Answers (1)

Pedro Lobito
Pedro Lobito

Reputation: 98951

Numbers cannot be accepted into the input but only letters.

I think this is a job for isinstance(), i.e.:

if isinstance(A, str):
    # A is a string

Upvotes: 1

Related Questions