Search code examples
pythonwhile-loop

If Condition Issues


I have made a simple game for python in a function called (game()). I made an IF condition within a While loop that asks a person if he wants to play the game again (yes=execute game(), no=break) but im facing an issue if the user didnt put neither yes nor no. how do I make it repeat the IF block until the user either put either yes or no. The code is shown as below. Thanks

`

while True:
     game()
     user=input("Do you want to play again? \n")
     if user=="yes":
        game()
     elif user=="no":
         print("Game is over")
         break
     else:
        ("Please Enter Yes or no") `

Solution

  • You can do something like:

    play = "yes"
    while play == "yes":
        game()
        play = "unknown"
        while play not in ["yes", "no"]:
            play = input("Play again (yes/no)? ").lower().strip()
    

    This does the following:

    • Assumes initial play value of yes to play the game at least once.
    • Ensure the user answers yes or no (ignoring case and leading or trailing white space).
    • Continues the loop only for the yes case.

    There's improvements you can make if, for example, you want a different message when user enters an invalid response, but that code should be a good start.


    If you do decide it needs more features, that's probably when you could argue it would be better in a helper function, something like:

    def ask(prompt, allowed):
        resp = input(prompt).lower().strip()
        while resp not in allowed:
            print(f"*** Not one of [{", ".join(allowed)}]")
            resp = input(prompt).lower().strip()
        return resp
    
    game()
    while ask("Play again (yes/no)? ", ["yes", "no"]) == "yes":
        game()
    

    Putting it into a helper function means that you can add all sorts of wonderful features as needs require, probably with default-value parameters so the most common case doesn't require them, just by changing that one function.

    For example, you may want to allow for a different subsequent prompt and making it case sensitive or not:

    def _ask_case(value, ignore_case):
        return value.lower() if ignore case else value
    
    def ask(prompt, allowed, prompt2=None, ignore_case=True):
        if prompt2 is None:
            prompt2 = prompt
        if ignore_case:
            allowed = [item.lower() for item in allowed]
        str_allow = f"*** Not one of [{', '.join(allowed)}]"
    
        resp = _ask_case(input(prompt).strip(), ignore_case)
        while resp not in allowed:
            print(str_allow)
            resp = _ask_case(input(prompt2).strip(), ignore_case)
    
        return resp
    

    I haven't tested that code (so it may not work) and it may not be immediately necessary for your purposes, but the idea is just to show that refactoring it into a helper function means you can use it everywhere, and very simply, but still have a lot of power if or when you need it.