144,386 views
44 votes
44 votes
What you need to know In this program, a roll string is a number, followed by a lowercase "d", followed by another number. The first number is the number of dice to roll. The second number is the number of sides on each die. For example, 1d6 means there is one die with six sides. The total output of the roll is a random whole number between one and six, inclusive. 2d8 means there are two dice, each with eight sides. The total output of the roll is a random whole number between two and 16, inclusive. Since we are using a computer, we can have an input like 500d1000 (meaning a 1000-sided die, rolled 500 times) which would not be possible in real life.

Function: get_roll(rollstring)
This function takes an input parameter as a string that looks like "1d3", "3d5", etc. The function should return an integer simulating those dice rolls. In the case of "3d5", for example, the function will generate a random number between one and five, three times, and return the total (sum) as an integer. This function should return the value back to get_damage().
Function: get_damage(attack, defense)
This function gets the value of damage based on roll strings. Player 1 (the first roll) is always on attack; Player 2 (the second roll) is always on defense. If the defense roll is greater than the attack roll, return 0. Otherwise, return attack minus defense, as an integer. This function should call get_roll() twice, once for each Player. This function should return the value back to main_main().
Function: main_menu()
In this function, ask the user how many rounds they want to play. Next, ask the user to input Player 1 and Player 2’s dice for roll one, roll two, etc., up to the number of rolls they entered. The rolls for each round must be entered on one line with no spaces. This function should call get_damage() for each round.

User Chinmay Mourya
by
2.6k points

1 Answer

6 votes
6 votes

Answer:

import random

def get_damage(attack,defense):

attack_roll = get_roll(attack)

defense_roll = get_roll(defense)

if defense_roll>attack_roll:

return 0

else:

return attack_roll-defense_roll

def main_menu():

rounds = int(input('Enter the number of rounds to play? '))

for round in range(1,rounds+1):

p1= input('Player 1 enter rollstring: ')

p2= input('Player 1 enter rollstring: ')

damage = get_damage(p1,p2)

print('Round: #{}, Damage: {}'.format(round,damage))

def get_roll(rollstring):

dices, sides = rollstring.split('d')

total = 0

for roll in range(int(dices)):

total += random.randint(1, int(sides))

return total

main_menu()

Step-by-step explanation:

The main_menu python function is the module dice game that prompts for user input to get the number of times a round is played to call the get_damage function which in turn calls the get_roll function to simulate the dice roll and return the accumulated total of the sum of the random dice value.

User Mikerowehl
by
2.8k points