0

I have the following piece of Python code:

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

What should I do to make sure they can't move on until they enter their name because at the moment they can move on without entering their name by pressing enter?

yprez
  • 14,854
  • 11
  • 55
  • 70
WolfSBanE
  • 1
  • 2
  • 2
    possible duplicate of [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – fredtantini Dec 09 '14 at 22:56
  • `while not PlayerName`? – jonrsharpe Dec 09 '14 at 23:06
  • @fredtantini: it is not a duplicate. The other question is related but it is much more complex and more general. [@AtlasC1's answer](http://stackoverflow.com/a/27391076/4279) is a good answer for this question but it won't be appropriate for the question you've linked. – jfs Dec 23 '14 at 22:51

1 Answers1

2

You want to prevent the program from continuing until they have entered their name. You also want to repeatedly ask for their name until they enter something other than an empty string. This sounds like a loop!

def GetPlayerName():  
   print()
   playerName = ""
   while not playerName.strip():
       playerName = input('Please enter your name: ')
       print()
   return playerName
Julian
  • 1,688
  • 1
  • 12
  • 19
  • 2
    I would make it: `while not playerName.strip():`, just to cover the case when you could press space and make it look like there is actually not a name there do display later. – bosnjak Dec 10 '14 at 11:38