0

Python newbie here, I am trying to create a simple login function that checks if the user's input is more than 3 characters and that it also doesn't contain a number.

The function does sort of work, because while if the user enters less than 3 characters it prints 'Your name must be more than 3 characters'.

What I can't figure out is how to check if the user enters a number then print 'Must not contain numbers'.

def login_function():
    print('****************************************')
    user_name = ""
    while len(user_name) <= 3: 
        user_name = input('Please enter your name: ')
        print('Your name must be more than 3 characters')
    return user_name

login_function()

I have tried using str(input('')) to make sure that the user only enters a character. I have also tried if else but it ended up quite messy.

trombonee
  • 31
  • 7
  • Are you aware of how to use `re.find` of the regex module? – OneCricketeer Mar 24 '21 at 18:38
  • You should also move the error above your input for a better user experience – OneCricketeer Mar 24 '21 at 18:39
  • Use re.findall: `import re while True: user_name = input('Please enter your name: ') if len(user_name) <= 3: print('Your name must be more than 3 characters') elif re.findall(r'\d', user_name): print('Your name must not contain any digits') else: break ` – Timur Shtatland Mar 24 '21 at 18:47
  • @OneCricketeer no I don't know the regex module, I am not that advanced with Python yet. My question has been closed because it's similar to another question but the answers don't make sense to me – trombonee Mar 24 '21 at 18:48
  • @TimurShtatland Thanks, that works nearly as intended but it doesn't work for when I enter a digit, it still prints `'Your name must be more than 3 characters'` `instead of 'Your name must not contain any digits'` – trombonee Mar 24 '21 at 18:57
  • @trombonee You can reverse the order of the conditions in the `if...elif ...else` statement, and make the test for digits the **first** test. – Timur Shtatland Mar 24 '21 at 18:59
  • @TimurShtatland Thank you, that has worked perfectly. – trombonee Mar 24 '21 at 19:09
  • You're asking how to check if a string contains a digit, and so does the other post. Can you clarify which answers don't make sense? If you're confused on usage of functions, refer to the Python documentation – OneCricketeer Mar 24 '21 at 22:43

0 Answers0