0
# importing random module, for random choices
import random

# print_move function to display CPU and players current move
def print_move(current_player, row, stick_quantity):
    current_player = current_player
    if stick_quantity == 1:
        print(f"{current_player} takes {stick_quantity} sticks from {row} row")
    else:
        print((f"{current_player} takes {stick_quantity} sticks from {row} row"))
        

# beginning of nims game, choosing who will choose  
print("Welcome to Nim!")
name = input("Enter name: ")
current_player = random.choice([name, "CPU"])
print('game is starting',(current_player),'will be going first')

# create stick lists
sticks = [1, 3, 5, 7]

# looping the sticks 
def nims():
    for count, stick in enumerate(sticks):
        print(count+1, stick * "|")

# loop that breaks when the number of sticks are less than 0
while sum(sticks) > 0:
    nims()

    if current_player == name:
# exception handling so that the user inputs the correct amount of sticks/rows
        while True:
            try:
                row = int(input("Which row would you like to take sticks from? "))
                stick_quantity = int(input("How many sticks would you like to take? "))
                if 1 <= row <= len(sticks) and 1 <= stick_quantity <= sticks[row-1]:
                    break
                else:
                    print("")
                    print("Invalid input, please enter a valid row, and or a valid amount of sticks")
            except ValueError:
                print("")
                print("Invalid input, please enter a valid row, and or a valid amount of sticks")

        sticks[row - 1] -= stick_quantity
        print_move(current_player,row,stick_quantity)
        if current_player == name:
            current_player = "CPU"
        else:
            current_player = name
    
    else:
        # handling value error in the stick_quantity variable
        try:
            row = random.randint(1, len(sticks))
            stick_quantity = random.randint(1, sticks[row-1])
            sticks[row-1] -= stick_quantity
            print_move(current_player,row,stick_quantity)
            if current_player == "CPU":
                current_player = name
            else:
                current_player = "CPU"
        except ValueError:
            print("")


winner = current_player
print("Winner is", winner)

I've created my Nims game in python, everything works well, except I am having an issue when it comes to repeating the loop again. I want to make it so that after the game ends, and the winner is announced, you will be asked again, would you like to play again, and if the user enters 'y' then the Nims game repeats again, but if the user presses on 'n' or anything else for that matter, the game ends and a score of how many wins and losses of the user will be displayed.

I'm not sure whether I should have a While play_again == 'y': kind of loop at the beginning of the nims game, i've tried doing that, but that doesn't seem to work. I've tried other things also, that I will not be explaining, but thus far, they haven't worked well enough for me. Any help or guidance will be appreciated. Thanks in advance.

1
  • Just a side note, why print("")? print() will do just the same, and so will do removing that line and just using print("\ninvalid...") Commented Mar 28, 2024 at 13:10

1 Answer 1

0

You could try something like this:

print("Welcome to Nim!") 
name = input("Enter name: ") 
replay = 'y' 

while replay == 'y':

and then at the end of the game:

winner = current_player print("Winner is", winner)
replay = str(input("One more round (y/n)?"))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.