In: Computer Science
Write code for a game of picking match sticks. You must make sure that the computer always wins. The game is as follows
There are 26 matchsticks initially. The computer will ask the user to pick anywhere between 1 and 4 matchsticks. The player (whether the user or the computer) who has to pick up the last matchstick (or matchsticks) is the loser. You will have to use a combination of branching and looping commands with appropriate prompts and the input() function.
Hint: The computer will always win if you code appropriately. When coding consider the following:
1. You must first print a statement to the screen which explains the game
2. You must print the remaining matchsticks after each round
3. You must finally print the final statement about who won the game.
In order to always ensure that the computer wins: the sum of the matchsticks chosen by the user and the computer should equal 5.
NB: Include picture of python code
print('Rules:')
print('1. There are 26 matchsticks initially')
print('2. In each turn, a player picks anywhere between 1 and 4
matchsticks')
print('3. The player who has to pick up the last matchstick (or
matchsticks) is the loser')
current = 26
k=1
loser=1
print('Matchsticks at the start:',current)
while current>0:
print('\nRound:',k)
user=int(input('Pick number of matchsticks: '))
current-=user
if current<=0:
loser=0
else:
print('Computer picks {} matchsticks'.format(5-user))
current-=(5-user)
if current<=0:
loser=1
else:
print('End of round {}, remaining matchsticks:
{}'.format(k,current))
k+=1
if loser==0:
print('You lose!! Computer wins')
else:
print('Computer loses! You win!!!')