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.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • 2
    Well, it could be helpful if you'd show us your solution so we can say _why_ it doesn't work. – TidB Jan 19 '15 at 20:07
  • Regarding the last edit: How would you expect the value of `PlayerName` to change in the loop? – TidB Jan 19 '15 at 20:11
  • 1
    Pretty close to being a duplicate of [this](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response). – jme Jan 19 '15 at 20:11
  • 1
    @jme I disagree. That question is about error handling. – HarryCBurn Jan 19 '15 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. – jme Jan 19 '15 at 20:24
  • @jme Ah, I see your point! ;P – HarryCBurn Jan 19 '15 at 20:26

2 Answers2

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.

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • 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 Jan 19 '15 at 20:21
  • @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.. – Anshul Goyal Jan 19 '15 at 20:24
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!
HarryCBurn
  • 755
  • 1
  • 8
  • 17
  • 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. – jme Jan 19 '15 at 20:26
  • 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.. – An0n Jul 18 '18 at 00:11