In: Computer Science
Python: Accumulator pattern and conditionals.
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.
*Write a function result that takes an arguments goal and score and
Prints the score. Then it prints one of the following:
o If the score at least twice the goal then the function should print “Nailed it!”
o If the score is at least the goal print “So so performance”
o Otherwise print “Not good… not good at all!”
import random # to generate random value
score = 0
def scoreCalc(dc1, dc2):
total_round_wise = dc1 +dc2 # round score
global score
score += dc1+dc2 # aggregate score calculate
return total_round_wise # return round score
def play(rounds, sides):
i=0
while(i != rounds):
ch = input("Press any key to roll the two dices.\n")
dice1 = random.randint(1,sides) # random number for dice 1
print("First dice: {}".format(dice1))
dice2 = random.randint(1,sides) # random number for dice 2
print("Second dice: {}".format(dice2))
print("Your score of round {} is: {}".format(i+1, scoreCalc(dice1, dice2))) # round score print
i += 1 # next round
def result(goal, final_score):
if(final_score >= int(2*goal)):
print("\nNailed it!")
elif(final_score >= goal and final_score < int(2*goal)):
print("\nSo so performance")
else:
print("Not good… not good at all!")
r = int(input("Input number of round: "))
s = int(input("Input number of sides of dice: "))
play(r, s) # calling play function
goal = int(input("Input your Goal: "))
print("Your total score is: {}".format(score))
result(goal, score) # call result function
OUTPUT: