Question

# Dice Game – Accumulator Pattern and Conditionals (40 pts) IN PYTHON Write a program dicegame.py...

Dice Game – Accumulator Pattern and Conditionals (40 pts) IN PYTHON

Write a program dicegame.py that has the following functions in the following order: in python

1. Write a function roll that takes an int n and returns a random number between 1 and n inclusive. Note: This function represents a n sided die so it should produce pseudo random numbers. You can accomplish this using the random module built into python.         (3 pts)

1. Write a function scoreRound that takes two dice rolls r1, r2, and a number s, and returns the score for a round when r1, r2 is rolled on with s-sided dice as follows:
1. Start the score with the sum of the r1 and r2
2. If one die is 2, and the other a 3, then add to 5 the score
3. If the two dice rolls r1, r2 match, and are not equal to s add 5 to the score. If both die rolls are s, then the score should be deducted by 3*s points.                            (5 pts)

1. Write a test function test_scoreRound to test the scoreRound function.                    (5pts) Use the test function to make sure your scoreRound function has no logical errors before continuing.
2. Write a function play that takes integers rounds and sides and does the following  Plays round number of rounds, rolling two s-sided dice per round (use roll).
• Uses the previous functions to calculate the score of each round
• Prints out the dice rolls and the score for each round
• Accumulates the score over all rounds
• Returns the final score over all rounds                                                             (5 pts)
3. Write a function result that takes an arguments goal and score and              Prints the score. Then it prints one of the following:
• If the score at least twice the goal then the function should print “Nailed it!” o If the score is between at least the goal print “So so performance”
• Otherwise print “Not good… not good at all!”                                             (5 pts)

1. Write a test function test_result to test the result function.                                        (5pts)

1. Write a main function that
1. Asks the user to enter the number of dice sides they like to use and the number of rounds they want to play.
2. Set the goal as round*(s+1)/2
3. Print a message to inform the user about their goal.
4. Then call the previous functions to play the game and display the result.               (5 pts)

1. Write a header comment for each function                                                                                      (7 pts)

Page 1 of 2

The Python 3.x Program is given below within the table. You are requested to create a python file named dicegame.py within the python ide and copy the below codes and paste into it and run the Program.

 dicegame.py ``````import random as r # Function -> roll(n) # This function calculates sudo random numbers in range 1 to n and return it, simulates rolling of n-sided dice. def roll(n): value = r.randint(1, n) return value # Function -> scoreRound(r1, r2, s) # This function calculates the score based on values of Roll r1, r2 and side s. # inputs: r1 = value of Roll 1; r2 = value of Roll 2; s = number of sides in dice # returns: the calculated score def scoreRound(r1, r2, s): score = r1 + r2 # calculate the score # check the conditions and add up to get the final score # if any of the values are 2 and 3 if (r1 == 2 and r2 == 3) or (r2 == 2 and r1 == 3): score = score + 5 # if both r1 and r2 are equal and are not equal to s elif (r1 == r2) and (r1 != s): score = score + 5 # if all the three values r1, r2, and s are equal elif (r1==s) and (r2==s): score = score - (3*s) return score # Function to test the scoreRound() function with different r1, r2, s values def test_scoreRound(): print('Score Test #1:\nr1 = 1, r2 = 3, s = 2;\nThe score should be 4\nThe actual score is = ', scoreRound(1,3,2),'\n') print('Score Test #2:\nr1 = 2, r2 = 3, s = 2;\nThe score should be 10\nThe actual score is = ', scoreRound(2,3,2),'\n') print('Score Test #3:\nr1 = 3, r2 = 2, s = 3;\nThe score should be 10\nThe actual score is = ', scoreRound(3,2,3),'\n') print('Score Test #4:\nr1 = 4, r2 = 4, s = 2;\nThe score should be 13\nThe actual score is = ', scoreRound(4,4,2),'\n') print('Score Test #5:\nr1 = 3, r2 = 3, s = 3;\nThe score should be (r1+r2)-(3*s) = 6-9 = -3. The actual score is = ', scoreRound(3,3,3)) # Function -> play(rounds, sides) # Function to play the dice game with given number of rounds and number sides in die # returns: total accumulated score of all the rounds played def play(rounds, sides): totalAccumulatedScore = 0 # loop for number of rounds for round_ in range(0, rounds): # roll the dice r1 = roll(sides) r2 = roll(sides) # get the score score = scoreRound(r1, r2, sides) # print the values print('Round #', (round_ +1)) print('Roll 1 = ', r1, 'Roll 2 = ', r2) print('Score = ', score) print() totalAccumulatedScore = totalAccumulatedScore + score return totalAccumulatedScore # Function -> goal(goal, score) # Function to dispay the result message based on goal and score def result(goal, score): if (score >= 2*goal): print('Nailed it!') elif (score >= goal): print('So so performance!') else: print('Not good… not good at all!') # Function to test the result() function def test_result(): print('Result Test #1:\nGoal = 10', 'Score = 20', 'Result = ', end='') result(10, 20) print('\nResult Test #2:\nGoal = 10', 'Score = 21', 'Result = ', end='') result(10, 21) print('\nResult Test #3:\nGoal = 10', 'Score = 19', 'Result = ', end='') result(10, 19) print('\nResult Test #4:\nGoal = 10', 'Score = 15', 'Result = ', end='') result(10, 15) print('\nResult Test #5:\nGoal = 10', 'Score = 10', 'Result = ', end='') result(10, 10) print('\nResult Test #6:\nGoal = 10', 'Score = 9', 'Result = ', end='') result(10, 9) print('\nResult Test #7:\nGoal = 10', 'Score = -3', 'Result = ', end='') result(10, -3) # calling the test_scoreRound() function test_scoreRound() print() # calling the test_result() function test_result() print() print('*************** G A M E B E G I N S ******************') # getting the number of sides from the user sides = int(input('Enter the number of sides of dice: ')) # getting the number of rounds to be played from user rounds = int(input('Enter the number of rounds to be played: ')) # calculating the goal value goal = rounds * (sides+1)/2 # display the goal to achieve print('The goal to achieve is: ', goal) # call the play function to play the game supplying the number of rounds and die-sides score = play(rounds, sides) print('Total score is = ', score) # display the result result(goal, score)``````

OUTPUT

Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32
>>>
========= RESTART: C:/Program Workspace/Python 3 Workspace/dicegame.py =========
Score Test #1:
r1 = 1, r2 = 3, s = 2;
The score should be 4
The actual score is = 4

Score Test #2:
r1 = 2, r2 = 3, s = 2;
The score should be 10
The actual score is = 10

Score Test #3:
r1 = 3, r2 = 2, s = 3;
The score should be 10
The actual score is = 10

Score Test #4:
r1 = 4, r2 = 4, s = 2;
The score should be 13
The actual score is = 13

Score Test #5:
r1 = 3, r2 = 3, s = 3;
The score should be (r1+r2)-(3*s) = 6-9 = -3. The actual score is = -3

Result Test #1:
Goal = 10 Score = 20 Result = Nailed it!

Result Test #2:
Goal = 10 Score = 21 Result = Nailed it!

Result Test #3:
Goal = 10 Score = 19 Result = So so performance!

Result Test #4:
Goal = 10 Score = 15 Result = So so performance!

Result Test #5:
Goal = 10 Score = 10 Result = So so performance!

Result Test #6:
Goal = 10 Score = 9 Result = Not good… not good at all!

Result Test #7:
Goal = 10 Score = -3 Result = Not good… not good at all!

*************** G A M E B E G I N S ******************
Enter the number of sides of dice: 5
Enter the number of rounds to be played: 3
The goal to achieve is: 9.0
Round # 1
Roll 1 = 4 Roll 2 = 3
Score = 7

Round # 2
Roll 1 = 2 Roll 2 = 4
Score = 6

Round # 3
Roll 1 = 4 Roll 2 = 4
Score = 13

Total score is = 26
Nailed it!
>>>

NOTE: Please comment your doubts(if any) within the comments section and please give a thumbs up if you liked the Program.