0

I have problem with users input control in one function in Python 3.4.

def input_name (*args):
    name_output = ""
    name_input = input ("Give first name: ")
    if name_input.isalpha() and name_input[0].isupper() == True:
        name_output += name_input
        return (name_output)
    else:
        print ("Wrong, do it again")
        input_name ()

name = input_name()
print(name.lower())

I am trying to catch users wrong input - so the name must be alphabetical and first letter must be uppercase. In future code I will create users login name with lowercase letters, so I am trying to print users name with small leters for login name. And there is problem.

  1. When I type name firs time well, it's ok
  2. When I type first time name with 1 lowercase letter (name) and then I write it correctly (Name), it tells me Error, I don't understand why. Can you tell me, what is my mistake?

Thank you very much for showing the path. Mirek

4
  • What exactly is the Error? Commented Sep 4, 2015 at 22:28
  • The Error is "AttributeError:'NoneType' object has no attribute 'lower'". Cdonts answer is great and solved the problem and everything is working now, but I am learnin coding, so now I would like to know, if is possible to repair my way I post here. Commented Sep 4, 2015 at 23:37
  • It's missing a return before input_name () Commented Sep 4, 2015 at 23:40
  • Thank you very much. I've got here better solution then was my and knowledge how to repair my code. Commented Sep 4, 2015 at 23:43

1 Answer 1

1

The error is caused by the last line. Since your input is wrong the first time, the function returns None, so name.lower() raises an exception. I wouldn't use recursion in this case.

def input_name():
    while True:
        name_input = input ("Give first name: ")
        if name_input.isalpha() and name_input[0].isupper():
            return name_input
        else:
            print ("Wrong, do it again")

name = input_name()
print(name.lower())

Hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

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.