2

I am a beginner trying to learn Python. First question.

Trying to find a way to ask users to input alphabets only. Wrote this but it doesn't work! It returns True and then skips the rest before continuing to the else clause. break doesn't work either.

Can someone point out why? I assume it's very elementary, but I'm stuck and would appreciate it if someone could pull me out.

while True:
    n = input("write something")
    if print(n.isalpha()) == True:
        print(n)
        break
    else:
        print("Has to be in alphabets only.")
hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
Zane
  • 23
  • 2

4 Answers4

1

Your problem here is the print function. print does not return anything, so your if statement is always comparing None to True.

while True:
    n = input("write something")
    if n.isalpha():
        print(n)
        break
    else:
        print("Has to be in alphabets only.")
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
1

Your statement should be if n.isalpha() == True:. print won't return anything and so the value is None. Then, you are comparing None with True

while True:
    n = input("write something")
    if n.isalpha() == True:
        print(n)
        break
    else:
        print("Has to be in alphabets only.")
0

I've fixed the bug, below is the updated code:

while True:
    n = input("write something: ")
    if n.isalpha() == True:
        print(n)
        break
    else:
        print("Has to be in alphabets only.")
Cool Breeze
  • 738
  • 2
  • 10
  • 26
-1

Don't use print(n.isaplha()), it will be always True. Remove print() and use only n.isalpha()
Try this:-

while True:
    n = input("write something")
    if print(n.isalpha()) == True:
        print(n)
        break
    else:
        print("Has to be in alphabets only.")