A user wants to create a dice game played against a computer.
The aim of the game is to get as close to a score of 21 as you can, without going over 21. If your score goes over 21 then you lose.
The player’s score starts at 0.
For each turn:
two dice (each numbered from 1 to 6) are rolled
the total of the two dice rolls is added to the player’s score
the value of each dice and the player’s new total is output
if the current score is less than 21, the player is asked if they would like to roll the dice again: if the player says yes, they get another turn; otherwise, the game ends.
At the end of the game, the program should work as follows:
if the final score is 21, output a message to say the player has won
if the final score is greater than 21, output a message to say the player has lost
if the final score is less than 21, the program generates a random number between 15 and 21 inclusive:
if this random number is greater than the player’s final score, output a message to say the player has lost
otherwise, output a message to say the player has won.
Figure 17 shows the output of a program that plays this dice game.
Figure 17
Roll 1: 1
Roll 2: 4
Current score: 5
Would you like to roll again? yes
Roll 1: 1
Roll 2: 6
Current score: 12
Would you like to roll again? yes
Roll 1: 1
Roll 2: 2
Current score: 15
Would you like to roll again? yes
Roll 1: 6
Roll 2: 1
Current score: 22
You lost!
Write a Python program to simulate this game.
The first line has been written for you in the answer grid
The dice rolls are carried out by the program generating random numbers between box 1 and 6. You will need to use the Python function random.randrange(a, b)
which generates a random integer in the range a to b starting at a but finishing one before b.
You should use indentation as appropriate, meaningful variable name(s) and Python syntax in your answer.