user2979303
user2979303

Reputation: 1

Game with 2-player

A game with 2 players. In this game, each player takes turn and removes between 1 to 5 objects from one of the three bags: A, B, C. The player who removes the last object wins the game. Here are the rules:
• Each bag contains 10 objects in the beginning of the game.
• The player can remove object(s) from a single bag at a time.
• The player cannot choose to remove 0 object(s). The number of objects has to be between 1 and 5 (inclusive).
• The game ends when all the bags are empty.

•Current player has to choose the bag first (A or B or C) and then the number of object(s) that have to be removed from that bag.
• You have to also check whether the user’s input is valid. Is it a valid bag (A or B or C)? Is it a valid number (between 1 and 5)? Also, are there enough objects in the bag? If any of these conditions are not met, ask the user for input again. (You need to use a loop to do this)
Example:
A B C
10 10 10 Player1 takes 5 objects from B.
10 5 10 Player2 takes 5 objects from C.
10 5 5 Player1 takes 3 objects from A.
7 5 5 Player2 takes 3 objects from B.
7 2 5 Player1 takes 5 objects from C.
7 2 0 Player2 takes 2 objects from A.
5 2 0 Player1 takes 1 object from A.
4 2 0 Player2 takes 3 object from A.
1 2 0 Player1 takes 1 objects from B.
1 1 0 Player2 takes 1 object from B.
1 0 0 Player1 takes 1 object from A.

*note:*I must only use loops or if conditions no lists or other

i've got this far:

A=5
B=5
C=5
total_objects=15
Player1=0
Player2=0

while total_objects>0:
    selected_bag=input("Choose the bag:")
    if selected_bag == A or B or C:
        print("Valid bag")
    else:
        selected_bag=input("Not a valid bag choose again:")


    removed=int(input("How many objects do you want to remove?"))
    if removed>5 or removed<1:
        int(input("You must enter between 1 and 5:"))

    elif removed == 1 or 2 or 3 or 4 or 5:
        if selected_bag == A:
            A-=removed
        if selected_bag == B:
            B-=removed
        if selected_bag == C:
            C-=removed

    if A>0:
        print("There are",A,"objects left")
    elif B>0:
        print("There are",B,"objects left")
    elif C>0:
        print("There are",C,"objects left")


print("You win!")

Upvotes: 0

Views: 5040

Answers (2)

abarnert
abarnert

Reputation: 365647

There are two big problems with this line:

if selected_bag == A or B or C:

The first problem, as aikid already explained, is that or doesn't work that way; you want in.


The second problem is that you want to check whether the user has entered any of the strings "A", "B", and "C", not whether he's entered whatever the values of the A, B, and C variables happen to be.

So, what you want here is:

if selected_bag in ('A', 'B', 'C'):

You repeat both of these problems again, and need to fix them again:

elif removed == 1 or 2 or 3 or 4 or 5:

… needs to be …

elif removed in (1, 2, 3, 4, 5):

… or maybe better…

elif 1 <= removed <= 5:

And then, within that:

    if selected_bag == A:
        A-=removed

… needs to be …

    if selected_bag == "A":
        A-=removed

… and the same for B and C.


Finally, this part:

if removed>5 or removed<1:
    int(input("You must enter between 1 and 5:"))

… doesn't do anything useful. It asks the user to enter another number, converts the result to an int, and then ignores it and falls through to if A>0: part. If you want to keep asking until the user gives a valid answer, you need some kind of loop. And of course you have to store that value somewhere for it to be useful. For example:

removed=int(input("How many objects do you want to remove?"))
while removed>5 or removed<1:
    removed = int(input("You must enter between 1 and 5:"))

And you have a very similar mistake with the selected_bag input part.


If you want to handle two players, you need to keep track of whose turn it currently is. An easy way to do that is to store a current_player variable, and update it each time through the loop. For example:

current_player = 2

while total_objects>0:
    current_player = 2 if current_player == 1 else 1
    print("Player", current_player, " is now up.")

    # all the existing code

print('Player", current_player, "wins!")

If you want to "flip a coin" to decide who goes first, you can just change the first line to:

import random
current_player = random.choice((1, 2))

Upvotes: 2

aIKid
aIKid

Reputation: 28242

That's not how you check items. This line:

if selected_bag == A or B or C:

Doesn't do what you thought it does. It evaluates A or B or C and then comparing it with your selected_bag.

Use the in operator instead. Also, what you need check is the string, not the variables:

if selected in ('A', 'B', 'C'):

As abarnert mentioned, you also need to change the others.

Also, change this line:

elif removed == 1 or 2 or 3 or 4 or 5:

And to loop properly, you need to continue when the condition is false:

else:
    selected_bag=input("Not a valid bag choose again:")
    continue

To a simple else statement.

else:

To be honest, there are a lot of problems with your code. @abarnert explains this far better than i can.

Upvotes: 3

Related Questions