-1

I am creating a program where the user must guess the random number between 1-20. But, the user must type their name first (or at least a character). I can't seem to force the user to type something. How do I force the user to type something? Here's my source code: https://gist.github.com/AmmarFMR/dca16caf88c4c0b2d1b10a97becf3592

I also faced the problem at the end when a question is prompted to the user, "Do you want to play again? (Y/N)". The user could bypass the choices by either pressing enter twice or characters other than Y/N twice. How do I fix this?

And, I am sure that my simple program could be written in a more effective or shorter way. Please feel free to suggest improvements.

  • Note that a [mcve] must be **in the question**, but probably you want something like https://stackoverflow.com/q/23294658/3001761 – jonrsharpe Sep 17 '17 at 17:17
  • You don't; you take what the user chooses to enter, and decide whether to ask for a different, more acceptable response. – chepner Sep 17 '17 at 17:18
  • Put the `input` in a `while` loop and keep looping until you get valid data. – Zach Gates Sep 17 '17 at 17:19
  • 2
    Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Yaroslav Surzhikov Sep 17 '17 at 17:19

1 Answers1

0

You can use:

nothing = True 
while nothing == True:
    print('What is your name?')
    name = input()
    if name != "":
        nothing = False

You can put all your code in a while loop and do the same thing or you can use break in the if statement to break the loop.