0

I am asking for an input from the user and it has to be a single character alphabet.

I tried this code.

ask = ''
while len(ask) != 1 and ask.isalpha() != True:
    ask = input(message).upper()

But this accepts any single number digit to break out of the while loop. I don't understand why.

I checked the ask.isalpha() on that single number and that returns False. So why does it break out of the loop.

Sorry for this question. This is very naïve but I couldn't find an answer.

Thank you.

Edit: Added the whole code.

5
  • you need == instead of != with the second part of condition. also, consider using do.. while loop to allow the first input. Commented Jan 22, 2021 at 6:46
  • I am sorry, but the == doesn't work for me. The loop doesn't even start and there's no prompt for use input after changing to ==. Commented Jan 22, 2021 at 7:11
  • the while loop is failing as ask fails to satisfy the loop criteria. set ask to some default value say a or use do.. while loop as suggested. Commented Jan 22, 2021 at 7:13
  • Sorry @KrishnaChaurasia But pls explain this do-while loop. I am not able to find any such loop for python. Commented Jan 22, 2021 at 7:16
  • my bad...python doesn't support do.. while loop. stackoverflow.com/a/65840480/5147259 should solve your problem. Commented Jan 22, 2021 at 7:20

3 Answers 3

1

You need an or operation and not and to achieve what you want. Also, you need to take user input first time before entering the while loop.

So your code should be:

ask = input(message).upper()
while len(ask) != 1 or ask.isalpha() != True:
    ask = input(message).upper()
Sign up to request clarification or add additional context in comments.

13 Comments

I am sorry, but the == doesn't work for me. The loop doesn't even start and there's no prompt for use input after changing to ==.
@Meet Edited my answer, try again
I tried this. Entered 1 for the first input prompt. And the program stopped.
it should stop since 1.isalpha() is False.
But I need the input to be a 1 character alphabet only. '1' is not an alphabet so requirement is to ask for input again.
|
0

You are using != which stand for not equal to so when you enter 1 digit number condition for while loop doesn't match so code stops. You should use == instead of !=.

1 Comment

I am sorry, but the == doesn't work for me. The loop doesn't even start and there's no prompt for use input after changing to ==.
0

There is different way to do it

# if you want user to enter upper case
while True:
    ask = input('message: ')
    if len(ask) == 1 and ask.isalpha() == True and ask.isupper() == True:
       break
       
# if you want to convert input to upper
while True:
    ask = (input('message: ')).upper()
    if len(ask) == 1 and ask.isalpha() == True:
       break

is this case all your condition are satisfied,

2 Comments

this is overcomplicated and doesn't really provide a solution to the question. other answers are more clear
please enlight me what i'm doing wrong, the question specify the need of single alpha character to break the loop, with the given the condition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.