For my programming assignment, I need to write a Python3 named “ matchstick” whi
ID: 3920283 • Letter: F
Question
For my programming assignment, I need to write a Python3 named “ matchstick” which plays a matchstick-picking game;Matchsticks 15; players 2; Would start a game with 15 sticks. Players take turns picking 1,2 or 3 sticks from a pile. Until all matchsticks are taken,and whoever has an even number of matchsticks wins
If no number is given , then ask the user to enter one. The program makes the first pick,then ask how many sticks they want to pick. Once there are no sticks left and count who has even matchsticks ,the program announces who won.
For my programming assignment, I need to write a Python3 named “ matchstick” which plays a matchstick-picking game;
Matchsticks 15; players 2; Would start a game with 15 sticks. Players take turns picking 1,2 or 3 sticks from a pile. Until all matchsticks are taken,and whoever has an even number of matchsticks wins
If no number is given , then ask the user to enter one. The program makes the first pick,then ask how many sticks they want to pick. Once there are no sticks left and count who has even matchsticks ,the program announces who won.
Matchsticks 15; players 2; Would start a game with 15 sticks. Players take turns picking 1,2 or 3 sticks from a pile. Until all matchsticks are taken,and whoever has an even number of matchsticks wins
If no number is given , then ask the user to enter one. The program makes the first pick,then ask how many sticks they want to pick. Once there are no sticks left and count who has even matchsticks ,the program announces who won.
Explanation / Answer
program:--
#define and initilize player1 score, player2 score and match number of sticks as dictionary
score = {'P1': 0, 'P2': 0, 'match_sticks': 15}
#ask user who want pick first
turn=input('Select who want to pick first (enter P1 for Player1, P2 for Player2 : ')
#game starts and repeat untill number of match sticks is 0
while score['match_sticks'] > 0:
#first print all the scores and remaining sticks
print('player1 Score is: ',score['P1'])
print('player2 Score is: ',score['P2'])
print('Remaining sticks: ',score['match_sticks'])
#for player1 turn
if turn == 'P1':
pick=int(input('1st Player: How many match sticks to pick: '))
while pick>score['match_sticks'] or pick == 0 or pick > 3:
pick=int(input('Your pick renge must be 0 <pick <= 3, and <= remaining sticks pick again: '))
score['P1']=score['P1']+pick #add number of picks to player1 score
score['match_sticks']=score['match_sticks']-pick #substract from number of sticks
turn='P2'
#for player2 turn
elif turn == 'P2':
pick=int(input('2nd Player: How many match sticks to pick: '))
while pick>score['match_sticks'] or pick == 0 or pick > 3:
pick=int(input('Your pick range must be 0 < pick <= 3, and <= remaining stick spick again: '))
score['P2']=score['P2']+pick #add number of picks to player1 score
score['match_sticks']=score['match_sticks']- pick #substract from number of sticks
turn='P1'
#if player1 has even sticks
if score['P1'] % 2 ==0:
print('Player1 wins')
else:
print('Player2 wins') #if player2 has even sticks
1)Out Put:--
============= RESTART: D:/Real-world/Python/Matchsticks_game.py =============
Select who want to pick first (enter P1 for Player1, P2 for Player2 : P1
player1 Score is: 0
player2 Score is: 0
Remaining sticks: 15
1st Player: How many match sticks to pick: 5
Your pick renge must be 0 <pick <= 3, and <= remaining sticks pick again: 3
player1 Score is: 3
player2 Score is: 0
Remaining sticks: 12
2nd Player: How many match sticks to pick: 0
Your pick range must be 0 < pick <= 3, and <= remaining stick spick again: 3
player1 Score is: 3
player2 Score is: 3
Remaining sticks: 9
1st Player: How many match sticks to pick: 3
player1 Score is: 6
player2 Score is: 3
Remaining sticks: 6
2nd Player: How many match sticks to pick: 3
player1 Score is: 6
player2 Score is: 6
Remaining sticks: 3
1st Player: How many match sticks to pick: 2
player1 Score is: 8
player2 Score is: 6
Remaining sticks: 1
2nd Player: How many match sticks to pick: 2
Your pick range must be 0 < pick <= 3, and <= remaining stick spick again: 0
Your pick range must be 0 < pick <= 3, and <= remaining stick spick again: 3
Your pick range must be 0 < pick <= 3, and <= remaining stick spick again: 1
Player1 wins
>>>
2) output:--
Select who want to pick first (enter P1 for Player1, P2 for Player2 : P1
player1 Score is: 0
player2 Score is: 0
Remaining sticks: 15
1st Player: How many match sticks to pick: 3
player1 Score is: 3
player2 Score is: 0
Remaining sticks: 12
2nd Player: How many match sticks to pick: 2
player1 Score is: 3
player2 Score is: 2
Remaining sticks: 10
1st Player: How many match sticks to pick: 1
player1 Score is: 4
player2 Score is: 2
Remaining sticks: 9
2nd Player: How many match sticks to pick: 3
player1 Score is: 4
player2 Score is: 5
Remaining sticks: 6
1st Player: How many match sticks to pick: 2
player1 Score is: 6
player2 Score is: 5
Remaining sticks: 4
2nd Player: How many match sticks to pick: 1
player1 Score is: 6
player2 Score is: 6
Remaining sticks: 3
1st Player: How many match sticks to pick: 1
player1 Score is: 7
player2 Score is: 6
Remaining sticks: 2
2nd Player: How many match sticks to pick: 2
Player2 wins
>>>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.