Why is this code giving me an error Write a Python program t

Why is this code giving me an error?

Write a Python program to play a simplified version of the Nim game. Generate a pile of between 15 and 21 inclusive matchsticks using the random number generator. Allow the human player to go first and remove from 1 to 3 sticks. Then allow the computer to generate a random number between 1 and 3 inclusive and remove that many matchsticks(exception example: if the number is 3 and there are only 2 matchsticks left, then only remove 1 matchstick). Playalternates until 1 matchstick is left and the losing player removes it. Declare the winner and ask if the user would like to play another game.

Validate the input and write the following functions:

-computer_play(pile) –figures out how many sticks the computer will remove and returns the number of sticks to remove

-player_play(pile) –solicits input from the user as to how many sticks to remove, validates the input, and returns the number of sticks the player wants to remove

-print_gameboard(pile) –prints a vertical bar for each matchstick on one line with one space between each matchstick.

-main () –main function which allows the user to play the game as many times as desired; validates all input

Please go to the web site, http://www.archimedes-lab.org/game_nim/play_nim_game.html, and play the game to learn it’s rules.

((((((THIS IS THE CODE)))))):

Solution

Please let me know if you need more information:-

=======================================

import random


def computer_play(pile):
    return random.randint(1, 3)


def player_play(pile):
    numSticks = input(str(\'Number of sticks do you want to remove(1 to 3): \'))
    while int(numSticks) < 1 or int(numSticks) > 3:
        numSticks = input(str(\'Invalid input. Try again: \'))
    return numSticks


def print_gameboard(pile):
    for i in range(pile):
        print (\'| \'),
    print


def main():
    while True:
        pile = random.randint(15, 21)
        while True:
            print_gameboard(pile)
            if pile == 1:
                print(\"You have won the game\")
                break
            num = int(computer_play(pile))
            if num > pile:
                num = pile - 1
            pile -= num
            print(\'Computer removed \' + str(num) + \' piles\')
            print_gameboard(pile)
            if pile == 1:
                print(\"You have lost the game\")
                break
            num = int(player_play(pile))
            if num > pile:
                num = pile - 1
            pile -= num
            print(\'You removed \' + str(num) + \' piles\')

       play_again = input(\'\ Do you want to play again(y for yes, n for no)? \')
        if play_again != \'y\' and play_again != \'Y\':
            break

main()

=================================================================

Note:-

===============

1. ) raw_input : is deprecated it wont be available in new pythonversions

2.) you can not compare sting with integer so you need to explicilitly convert from string to int.

Please check the bolded parts which i have corrected.

=====================================

SAMPLE_OUTPUT:-

=========================================

=================================================================

Note:-

===============

1. ) raw_input : is deprecated it wont be available in new pythonversions

2.) you can not compare sting with integer so you need to explicilitly convert from string to int.

Please check the bolded parts which i have corrected.

=====================================

SAMPLE_OUTPUT:-

=========================================                                                                                                                

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Computer removed 2 piles                

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Number of sticks do you want to remove(1 to 3): 2                                                                                            

You removed 2 piles                                        

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Computer removed 1 piles         

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Number of sticks do you want to remove(1 to 3): 3                                                                                            

You removed 3 piles                                                                                                                          

|                     

|
|
|
|
|
|
|
|
|
|
|
|
Computer removed 2 piles                                                                                                                     

|
|
|
|
|
|
|
|
|
|
|
Number of sticks do you want to remove(1 to 3): 2

You removed 2 piles                                                                                                                          

|
|
|
|
|
|
|
|
|
Computer removed 1 piles                                                                                                                     

|
|
|
|
|
|
|
|
Number of sticks do you want to remove(1 to 3): 3                                                                                            

You removed 3 piles                                        

You removed 3 piles                                                                                                                          

|
|
|
|
|
Computer removed 2 piles                                                                                                                     

|
|
|                                                                                                                                        

Number of sticks do you want to remove(1 to 3): 1                                                                                            

You removed 1 piles                                                                                                                          

|
|
Computer removed 1 piles                                                                                                                     

|
You have lost the game
Do you want to play again(y for yes, n for no)? no                                                                                             ================================
Thanks

Why is this code giving me an error? Write a Python program to play a simplified version of the Nim game. Generate a pile of between 15 and 21 inclusive matchst
Why is this code giving me an error? Write a Python program to play a simplified version of the Nim game. Generate a pile of between 15 and 21 inclusive matchst

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site