2

I have some code:

def GetPlayerName():
  print()
  PlayerName = input('Please enter your name: ')
  print()
  return PlayerName

How can I keep asking for the player's name until they enter a name that is more than one character long, and tell them they must enter a valid name if they leave the field blank?

I tried

def GetPlayerName():
  print()
  PlayerName = input('Please enter your name: ')
  print()
  return PlayerName
  while len(PlayerName) < 1:
    print("You must enter a name!")

but have been unsuccessful.

6
  • 2
    Well, it could be helpful if you'd show us your solution so we can say why it doesn't work. Commented Jan 19, 2015 at 20:07
  • Regarding the last edit: How would you expect the value of PlayerName to change in the loop? Commented Jan 19, 2015 at 20:11
  • 1
    Pretty close to being a duplicate of this. Commented Jan 19, 2015 at 20:11
  • 1
    @jme I disagree. That question is about error handling. Commented Jan 19, 2015 at 20:13
  • @Iplodman Read the section titled: "Implementing Your Own Validation Rules". Swap the requirement that the input is in all caps for the requirement that the length is greater than one, and you have this question. Commented Jan 19, 2015 at 20:24

2 Answers 2

3

Use a while loop to get the input repetitively:

def get_player_name():
    print()
    player_name = ""
    while len(player_name) <= 1: 
        player_name = input('Please enter your name: ')
        print()
    return player_name

The way you are currently using it, you use the while statement to only print the error message.

PS: I've converted your variable names etc to small_caps_format because that is what PEP recommends.

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

2 Comments

The question wanted to ensure len(player_name) > 1, so you'll need <= rather than < in the while's predicate. Also, it would be nice to print an error message if the user inputs a bad name. It can be more succinct to do that if you use the while True idiom.
@jme thanks for the suggestions, made changes regarding the len > 1, As for the other, there is already another answer for the while True idiom..
2
def GetPlayerName():
    print()

    while True:
        PlayerName = input('Please enter your name: ')

        if len(PlayerName) > 1:
            break
        print("Your name is too short! :c")

    print()
    return PlayerName

One solution amongst others, and doesn't require any variables outside of the while loop. As mentioned by @jme, the error message is rather easy to print with this solution. The issue with your code is that:

  1. Your while loop is after the return statement is called, so it's affectively rendered mute.
  2. Your while loop is infinite-- it doesn't give the user a chance to re-try!

2 Comments

A single line can be added to this solution to print an error message on invalid input. The other solutions require a bit more work. That might be an advantage worth mentioning in your answer.
Whats the point of the while True if you don't have a continue in it ? That way you get big problems when working on bigger programs. But i like your style tho..

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.