Search code examples
pythonwhile-loopisalpha

How to use str.isalpha()?


This is what i have tried.

I insert 'Sharon' as my input but it still keeps printing "Not in alphabets". What should I do to print("In alphabets") while my input is in alphabets?


Solution

  • While the statement of the while loop is satisfied, the code in the while loop keeps running.

    With your code is like you are saying to the computer: While the name variable value is not equal to the Boolean value True, if name is composed of alphabet elements, or is not equal to the Boolean value False, if name is not composed of alphabet elements, keep printing "Not in alphabets".

    As you can understand, a strings variable value can never be equal to a Boolean value, thus the while loop never ends.

    You need an if else, not a while.

    if name.isalpha() != False:
        print( "Not in alphabets")
    else:
        print("In alphabets")
    

    ALSO: Post written code inside ` and not as image!