'How can I repeat a game over and over, for example guess game

I have seen many ideas about how to reply a game but none of them worked for me.

import random
list_number=["","","o"]
random.shuffle(listing)
def player_choice():
    player=""
    while player not in ["0","1","2"]:
        player=input("Please select a number from 0 to 2 : ")
    return int(player)
player_choice()

def player_guess(list_number,player_choice):
    play_again = "y"
    while play_again == "y":
        if player_choice == list_number[0]:
            print("You did it")
        elif player_choice == list_number[1]:
            print("You did it")
        elif player_choice == list_number[2]:
            print("You did it")
        else:
            print("You got it wrong. Do you want to try again?")
        play_again=input("Do you want to play again? y/n ")
        if play_again == "n":
            break
     

player_guess(listing,player_choice)

This is my code. I am new in this world of programming. I do not understand why it not keep repeating the game over and over even if I write "y" on "play_again". I have tried many ideas but none of them worked.



Solution 1:[1]

You have some code I could not understand as it seemed unfinished which may be where your issue was, but as I could not run your code I revised it to work, so this should give you what you want in terms of an optionally iterating command line program:

import random
list_number=["0","1","2"] 
def player_choice():
    player= None
    while player not in ["0","1","2"]:
        player=input("Please select a number from 0 to 2 : ")
    return player
    
def player_guess():
    # list_number is in global scope here
    play_again = "y"
    while play_again == "y":
        choice = player_choice()    # redefine each loop
        if choice == list_number[0]:
            print("You did it, you selected player: 0")
        elif choice == list_number[1]:
            print("You did it, you selected player: 1")
        elif choice == list_number[2]:
            print("You did it, you selected player: 2")
        else:
            print("You got it wrong. Do you want to try again?")
        play_again=input("Do you want to play again? y/n ")
        
        # note this code below is not needed at all, 
        # the value is checked in each iteration. If anything
        # but 'y' is entered it will end the while loop
        
        # if play_again == "n":
        #    break

player_guess()

# TODO: I would write the list_number items to a dict 
# and define the "You Did It" text in that,
# doing this would make your code much easier...
# think something like this to make your program a bit smarter:

def dict_options():
    my_dict = {'y':'You Entered Yes!', 'n': 'Boo :(' }
   
    while True:
        val = input('enter a value (y,n)')
        if val.lower() in ['y', 'n']:
            print(my_dict[val])
            
        play_again = input("Do you want to play again? y/n ")
        if not play_again.lower() == 'y':
            break

dict_options()

Although note that if your posted code could run your loop would end correctly as it was written. The infinite loop you describe is actually not possible unless you entered 'y'.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1