Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8835529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:16:49+00:00 2026-06-14T09:16:49+00:00

I have to do a small game called NIM. The game is a human

  • 0

I have to do a small game called NIM. The game is a human vs. computer game where each player removes a number of straws (1,2 or 3) and the player who removes the last straw looses. I got the game to work properly but the problem is that it doesn’t want to re-run if the player wants to play again. Any help would be apprecieted. 🙂

import random
print("""************ NIM GAME   ***********
************ Game Start ***********
************ The rules  ***********
-----------------------------------------------------
You need to remove from 1 to 3 straws from the pile.
The player that removes the final straw is the loser.
-----------------------------------------------------""")

player1=str(input("Enter your name. "))
player2="Computer"
howMany=0
gameover=False
strawsNumber=random.randint(10,20)

if (strawsNumber%4)==1:
    strawsNumber+=1

def removingStrawsComputer():
    removedNumber=random.randint(1,3)
    global strawsNumber
    while removedNumber>strawsNumber:
    removedNumber=random.randint(1,3)
    strawsNumber-=removedNumber
    return strawsNumber

def removingStrawsHuman():
    global strawsNumber
    strawsNumber-=howMany
    return strawsNumber

def humanLegalMove():
    global howMany
    legalMove=False
    while not legalMove:
        print("It's your turn, ",player1)
        howMany=int(input("How many straws do you want to remove?(from 1 to 3) "))
        if  howMany>3 or howMany<1:
            print("Enter a number between 1 and 3.")
        else:
            legalMove=True
    while howMany>strawsNumber:
        print("The entered number is greater than a number of straws remained.")
        howMany=int(input("How many straws do you want to remove?"))
    return howMany

def checkWinner(player):
    if strawsNumber==0:
        print(player," wins.")
        global gameover
        gameover=True
        return gameover

def resetGameover():
    global gameover
    gameover=False
    return gameover

def game():
    while gameover==False:
        print("It's ",player2,"turn. The number of straws left: ",removingStrawsComputer())
        checkWinner(player1)
        if gameover==True:
            break
        humanLegalMove()        
        print("The number of straws left: ",removingStrawsHuman())
        checkWinner(player2)

def playAgain():
    answer=input("Do you want to play again?(y/n)")
    resetGameover()
    while answer=="y":
        game()
    else:
        print("Thanks for playing the game")

game()
playAgain()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:16:51+00:00Added an answer on June 14, 2026 at 9:16 am

    You forgot to reset the number of straws at the start of each game. After def game():, you should insert:

    global strawsNumber
    strawsNumber=random.randint(10,20)
    

    Note: you also need to put answer=input("Do you want to play again?(y/n)") at the end of your while answer=="y": loop. This will ask the user for a replay every time, instead of just after the first game.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a game where i have small particles. Their number is changing
I have small game engine written in c++. I'm considering using some features of
I currently have a small text game I've written in Java that utilizes System.out.print();
I have writing a small shooting game in cocos2d. Winning the game is to
I have am writing a small game using Applet. I want to be able
I am creating a small Yahtzee game and i have run into some regex
I am current making some small JS game in Netbeans and I have started
I have developed a small administration page for game servers which you can add/remove/edit
I have a game engine that uses OpenGL for display. I coded a small
I have started development on a small 2d adventure side view game together with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.