In: Computer Science
Write a program that allows 2 players to play the marble game using combinations of loop and conditional statements. In the marble game, you ask the user to enter how many marbles to start with. Then, the game begins. The first player must take 1, 2 or 3 marbles. Then the second player goes and must take 1, 2 or 3 marbles. The winner is the player who takes the last marble. Allow two users to play this game and print out the winner (player #1 or player#2). Assume that both players enter valid inputs (1, 2 or 3, and they never try to take more marbles than there are in the pile.)
def pickMarbles(numMarbles, player): print('Player', player, ', How many marbles you want to pick out of', numMarbles) x = int(input('Enter marbles to be picked: ')) while(x < 1 or x > 3 or x > numMarbles): print('Invalid input. Try again.') x = int(input('Enter marbles to be picked: ')) print() return x numMarbles = int(input('Enter number of marbles: ')) turn = 2 # to start with first person. while(numMarbles > 0): turn = 1 if turn == 2 else 2 numMarbles -= pickMarbles(numMarbles, turn) print('Player', turn, 'won!')
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.