Question

In python this is what I have for task 3, but I don't know how to...

In python

this is what I have for task 3, but I don't know how to fix it so that the computer stops picking from the pile once it reaches zero

Task 3: Extend the game to have two piles of coins, allow the players to specify which pile they take the coins from, as well as how many coins they take Finish Task 3 and try three test cases

import random

remaining_coins1 = 22
remaining_coins2 = 22

while 1:

print("\nTake turns removing 1,2, or 3 coins.")
print("You win if you take the last coin.")

while remaining_coins1 > 0 or remaining_coins2 > 0:
print("\nThere are ", remaining_coins1, "coins remaining in pile 1.")
print("There are ", remaining_coins2, "coins remaining in pile 2.")

taken_pile = random.randint(1,2)

while 1:
if taken_pile == 1 and remaining_coins1 == 0:
taken_pile = 2
elif taken_pile == 2 and remaining_coins2 == 0:
taken_pile = 1
break

print("\nPlayer 1 (computer) has choosen pile ",taken_pile)

taken_coins = random.randint(1,3)

while 1:
if taken_pile == 1:
while taken_coins > remaining_coins1:
taken_coins = random.randint(1,remaining_coins1)
elif taken_pile == 2:
while taken_coins > remaining_coins1:
taken_coins = random.randint(1,remaining_coins2)
break

print("Player 1 (computer): How many coins do you take? ",taken_coins)

if taken_pile == 1:
remaining_coins1 = remaining_coins1 - taken_coins
else:
remaining_coins2 = remaining_coins2 - taken_coins

print("\nThere are ", remaining_coins1, "coins remaining in pile 1.")
print("There are ", remaining_coins2, "coins remaining in pile 2.")

if remaining_coins1 == 0 and remaining_coins2 == 0:
print("\nNo more coins left!")
print("Player 1 (computer) wins!")
print("Player 2 loses!")
print("\n --------------------------------")
break

# Player 2 turn
taken_pile = int(input("\nPlayer 2: Choose the coin pile ? "))
while taken_pile < 1 or taken_pile > 2:
print("That's not a legal move. Try again.")
taken_pile = int(input("Player 2: Choose the coin pile ? "))

while 1:
if taken_pile == 1 and remaining_coins1 == 0:
print("There is no coin in the choosen pile 1 so choose from pile 2")
taken_pile = 2
elif taken_pile == 2 and remaining_coins2 == 0:
print("There is no coin in the choosen pile 2 so choose from pile 1")
taken_pile = 1
break


taken_coins = int(input("Player 2: How many coins do you take? "))
while 1:
if taken_pile == 1:
while taken_coins < 1 or taken_coins > 3 or taken_coins > remaining_coins1:
print("That's not a legal move. Try again.")
print("\nThere are ", remaining_coins1, "coins remaining in pile 1.")
print("There are ", remaining_coins2, "coins remaining in pile 2.")
taken_coins = int(input("Player 2: How many coins do you take? "))
elif taken_pile == 2:
while taken_coins < 1 or taken_coins > 3 or taken_coins > remaining_coins2:
print("That's not a legal move. Try again.")
print("\nThere are ", remaining_coins1, "coins remaining in pile 1.")
print("There are ", remaining_coins2, "coins remaining in pile 2.")
taken_coins = int(input("Player 2: How many coins do you take? "))
break;

if taken_pile == 1:
remaining_coins1 = remaining_coins1 - taken_coins
else:
remaining_coins2 = remaining_coins2 - taken_coins

if remaining_coins1 == 0 and remaining_coins2 == 0:
print("\nNo more coins left!")
print("Player 2 wins!")
print("Player 1 (computer) loses!")
print("\n --------------------------------")
break


choice = input("\nDo you want to continue ?(y/n) ")
if choice == "n":
print("\nThank you for joining the game !");
exit()
elif choice == "y":
remaining_coins1 = random.randint(20,30)
remaining_coins2 = random.randint(20,30)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code is working fine.

The reason you are not getting any result once any of the coin pile reaches to 0 is because of the following:

1. There is a logical mistake in this part of the code that is why the console is not display any player win

if remaining_coins1 == 0 and remaining_coins2 == 0:
print("\nNo more coins left!")
print("Player 1 (computer) wins!")
print("Player 2 loses!")
print("\n --------------------------------")

In this code there is a mistake of and logical operator, here it means that to run this section of code both the condition should be true but it is not possible, the game is about who's so ever first empty the pile is a winner so instead of and there should be or logical operator ( the first player to empty the pile is the winner )

So i slightly modified this section of your code like this

So here i also introduced a new variable called win which is acting like a flag when any player wins it is set to 1 otherwise it default value is 0 ( the full code is given below )

2. I also delete some part of code which i think is not required, just to optimize it.

while 1:
if taken_pile == 1 and remaining_coins1 == 0:
taken_pile = 2
elif taken_pile == 2 and remaining_coins2 == 0:
taken_pile = 1
break

this while loop is not required as we are using random function for computer to select pile and coins and is also not running because of and operator because if remaining_coins1 == 0 then some player is a winner.

Full Code:

import random

remaining_coins1 = 22

remaining_coins2 = 22

win=0

while 1:

    print("\nTake turns removing 1,2, or 3 coins.")

    print("You win if you take the last coin.")

    while remaining_coins1 > 0 or remaining_coins2 > 0:

        print("\nThere are ", remaining_coins1, "coins remaining in pile 1.")

        print("There are ", remaining_coins2, "coins remaining in pile 2.")

        break

    

    taken_pile = random.randint(1,2)

    print("\nPlayer 1 (computer) has choosen pile ",taken_pile)

    

    taken_coins = random.randint(1,3)

   

    print("Player 1 (computer): How many coins do you take? ",taken_coins)

    if taken_pile == 1:

        remaining_coins1 = remaining_coins1 - taken_coins

    else:

        remaining_coins2 = remaining_coins2 - taken_coins

    print("\nThere are ", remaining_coins1, "coins remaining in pile 1.")

    print("There are ", remaining_coins2, "coins remaining in pile 2.")

    if(win==0):

        if(remaining_coins1 == 0 or remaining_coins2 == 0):

            print("\nNo more coins left!")

            print("Player 1 (computer) wins!")

            print("Player 2 loses!")

            print("\n --------------------------------")

            win=1

    # Player 2 turn

    taken_pile = int(input("\nPlayer 2: Choose the coin pile ? "))

    while taken_pile < 1 or taken_pile > 2:

        print("That's not a legal move. Try again.")

        taken_pile = int(input("Player 2: Choose the coin pile ? "))

        break

    while 1:

        if taken_pile == 1 and remaining_coins1 == 0:

            print("There is no coin in the choosen pile 1 so choose from pile 2")

            taken_pile = 2

        elif taken_pile == 2 and remaining_coins2 == 0:

            print("There is no coin in the choosen pile 2 so choose from pile 1")

            taken_pile = 1

        break

   

    taken_coins = int(input("Player 2: How many coins do you take? "))

    while 1:

            if taken_pile == 1:

                while taken_coins < 1 or taken_coins > 3 or taken_coins > remaining_coins1:

                    print("That's not a legal move. Try again.")

                    print("\nThere are ", remaining_coins1, "coins remaining in pile 1.")

                    print("There are ", remaining_coins2, "coins remaining in pile 2.")

                    taken_coins = int(input("Player 2: How many coins do you take? "))

            elif taken_pile == 2:

                while taken_coins < 1 or taken_coins > 3 or taken_coins > remaining_coins2:

                    print("That's not a legal move. Try again.")

                    print("\nThere are ", remaining_coins1, "coins remaining in pile 1.")

                    print("There are ", remaining_coins2, "coins remaining in pile 2.")

                    taken_coins = int(input("Player 2: How many coins do you take? "))

            break

    

    if taken_pile == 1:

        remaining_coins1 = remaining_coins1 - taken_coins

    else:

        remaining_coins2 = remaining_coins2 - taken_coins

    

    

    if(win==0):

        if remaining_coins1 == 0 or remaining_coins2 == 0:

            print("\nNo more coins left!")

            print("Player 2 wins!")

            print("Player 1 (computer) loses!")

            print("\n --------------------------------")

            win=1

    if(remaining_coins1==0 or remaining_coins2==0):

        choice = input("\nDo you want to continue ?(y/n) ")

        if choice == "n":

            print("\nThank you for joining the game !")

            exit()

        elif choice == "y":

            remaining_coins1 = random.randint(20,30)

            remaining_coins2 = random.randint(20,30)

        

NOTE: PLEASE BE CAREFUL OF INDENTS

Output:(the code is run in vscode)

Thank You

Add a comment
Know the answer?
Add Answer to:
In python this is what I have for task 3, but I don't know how to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can you please enter this python program code into an IPO Chart? import random def menu():...

    Can you please enter this python program code into an IPO Chart? import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the non-numbers #if user enters letters, then except will show the message, Numbers only! try: c=int(input("Enter your choice: ")) if(c>=1 and c<=3): return c else: print("Enter number between 1 and 3 inclusive.") except: #print exception print("Numbers Only!")...

  • Can you add code comments where required throughout this Python Program, Guess My Number Program, and...

    Can you add code comments where required throughout this Python Program, Guess My Number Program, and describe this program as if you were presenting it to the class. What it does and everything step by step. import random def menu(): #function for getting the user input on what he wants to do print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the...

  • (In Python 3) Write a program with total change amount as an integer input, and output...

    (In Python 3) Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. Ex: If the input is: 0 (or less than 0), the output is: No change Ex: If the input is: 45 the output is: 1 Quarter 2 Dimes So...

  • Case Study Baseball Team Manager. CMSC 135 Python Chapter 7 Assignment: Use a file to save...

    Case Study Baseball Team Manager. CMSC 135 Python Chapter 7 Assignment: Use a file to save the data Update the program so it reads the player data from a file when the program starts andwrites the player data to a file anytime the data is changed What needs to be updated: Specifications Use a CSV file to store the lineup. Store the functions for writing and reading the file of players in a separate module than the rest of the...

  • The game of Nim: This is a well-known game with a number of variants. The following...

    The game of Nim: This is a well-known game with a number of variants. The following variant has an interesting winning strategy. Two players alternately take marbles from a pile. In each move, a player chooses how many marbles to take. The player must take at least one but at most half of the marbles. Then the other player takes a turn. The player who takes the last marble loses. Write a C program in which the computer plays against...

  • Chapter 8 Python Case study Baseball Team Manager For this case study, you’ll use the programming...

    Chapter 8 Python Case study Baseball Team Manager For this case study, you’ll use the programming skills that you learn in Murach’s Python Programming to develop a program that helps a person manage a baseball team. This program stores the data for each player on the team, and it also lets the manager set a starting lineup for each game. After you read chapter 2, you can develop a simple program that calculates the batting average for a player. Then,...

  • on python i need to code a guessing game. After the player has guessed the random...

    on python i need to code a guessing game. After the player has guessed the random number correctly, prompt the user to enter their name. Record the names in a list along with how many tries it took them to reach the unknown number. Record the score for each name in another list. When the game is quit, show who won with the least amount of tries. this is what i have so far: #Guess The Number HW assignment import...

  • how do I write this code without the imports? I don't know what pickle is or...

    how do I write this code without the imports? I don't know what pickle is or os.path import pickle # to save and load history (as binary objects) import os.path #to check if file exists # character value mapping values = {'A': 1, 'B': 3, 'C': 3, 'D': 2, 'E': 1, 'F': 4, 'G': 2, 'H': 4, 'I': 1, 'J': 8, 'K': 5, 'L': 1, 'M': 3, 'N': 1, 'O': 1, 'P': 3, 'Q': 10, 'R': 1, ' S': 1,...

  • In python how could I update my code to tell me the number of guesses it...

    In python how could I update my code to tell me the number of guesses it took my to get the correct number here is my working code so far import random number = random.randint(1,100) total_guess = 0 while total_guess<=100: guess = int(input("what is your guess?")) print (guess) total_guess = total_guess + 1 if guess<number: print("Too low") if guess>number: print("Too high") if guess==number: print("Correct!") break

  • Hello, I am trying to solve the following problem using python: Assignment 3: Chatbot A chatbot...

    Hello, I am trying to solve the following problem using python: Assignment 3: Chatbot A chatbot is a computer program designed to emulate human conversation. For this program you will use if statements, user input, and random numbers to create a basic chatbot. Here is the scenario: You have decided to start an online website. You are creating a prototype to show investors so you can raise money and launch your website. You should ask the user at least 5...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT