0

I'm a very new programmer. Just starting out in Python. Essentially I have a to write a program that accepts username input, with some validation. The username has to be between 5-10 alphabetical characters. I'm getting the code to test for the length of the string, but I'm not getting it to test for alphabetical characters. What am I doing wrong?

correct = True
while correct:
    username = input('Enter a username that has only alphabetical characters and is between 5 and 10 characters long:')
    if username.isalpha:
        while len(username) < 5:
            print('Invalid username! Please try again.')
            username = input('Enter a username that has only alphabetical characters' +
                             ' and is between 5 and 10 characters long:')
        if username.isalpha:
            while len(username) > 10:
                print('Invalid username! Please try again.')
                username = input('Enter a username that has only alphabetical characters' +
                                 ' and is between 5 and 10 characters long:')
            correct = False
else:
    print('Username accepted.')
Neekolie
  • 23
  • 4

2 Answers2

1

isalpha is a function, btw you need to call it so do isalpha() instead

if you want to know more about python string https://docs.python.org/3/library/string.html, i suggest reading the official python documentation for better learning

0

As it is mentioned in the comment section, you missed the parenthesis () of isalpha.
I also suggest to edit the code like this:

while True:
    username = input('Enter a username that has only alphabetical characters and is between 5 and 10 characters long:')
    if username.isalpha() and 5 <= len(username) <= 10:
        print('Username accepted.')
        break
    else:
        print('Invalid username! Please try again.')
  • Oh my god. I was really overthinking it. I think my misuse of isalpha() led me to iterating the loops multiple times. Thank you so much. – Neekolie Oct 07 '21 at 19:09
  • I'm happy you find it useful, any upvote or correct answer ✅ is very appreciated @Neekolie – Behdad Abdollahi Moghadam Oct 07 '21 at 19:20
  • Happy to! I'm unfamiliar with the usage of the break statement, and how while True is possible without setting the variable. Does else statement act as 'false' in that case? And how does the loop still print the username input? I would have thought that print statement needs to be placed at the to achieve that. I'm a big time noob to programming and any insight is greatly appreciated. – Neekolie Oct 08 '21 at 05:11
  • **1)** The statement in the while condition will be finally converted to `True` and `False` so we can just put `True` in the condition of it, and when we meet a condition that should lead us through out of the `while loop`, we put `break`. **2)** while the `while loop` iterates over and over, first line of the `while loop` will take `input` from user. **3)** No, the text within the `input(..)` just do the same thing like the `print`, but it will take input after printing the text inside of it's parenthesis. @Neekolie – Behdad Abdollahi Moghadam Oct 08 '21 at 08:43