Reputation:
I'm trying to write a program which is similar to a D&D dice rolling game. It will prompt the user to input the amount of rolls they want to do, input their rolls, and output the result. There will be three main functions, get_damage: takes both user input and if the attack is higher than the defense, display the damage count. If the defense is greater, display damage as 0. get_roll: supposed to simulate a diceroll with an infinite possibility of die and faces, looking like 10d3 or 20d50 (100 die with 3 faces, or 20 die with 50 faces). It will pick a random number and add the sum of the rolls to display an integer. Lastly, I have the main_menu function which will asks the user the amount of rounds to play, take input, and display the intended ending.
For example, my intended exchange is to go something like this:
How many rolls do you want to do / rounds to play? : (User input, ex 2) Input player 1 attack and player 2 defense for roll 1: (User input, ex 10d4,5d3) Input player 1 attack and player 2 defense for roll 2: (User input, ex 4d6,1d600)
Output: Player 1 attack: 10d4, Player 2 defense: 5d3,Damage : 0 Player 1 attack: 4d6, Player 2 defense: 1d600,Damage : 12
The problem is, I'm getting an error that says "ValueError: empty range for randrange() (10, 5, -5)". My code is attached below. Could anyone shed some insight into this?
def get_damage(attack, defense):
if attack > defense:
return attack - defense
else:
return 0
def get_roll(rollstring):
repeat = int(rollstring.split("d")[0])
torandom = int(rollstring.split("d")[1])
for i in range(repeat):
complete = random.randint(repeat,torandom)
return complete
def main_menu():
a = int(input("How many rolls do you want to do? "))
c = 0
list = []
for roll in range (0, a):
print("Input Player 1 and Player 2's rolls for round", roll + 1, ": ")
b = input()
list = list + b.split(",")
for element in range (0, a):
attack = list[c]
defense = list[c + 1]
attack1 = get_roll(attack)
defense1 = get_roll(defense)
print("Player 1 Attack: " + str(attack) + ", " + "Player 2 Defense: " + str(defense) + ", " + ">> Damage: " + str(get_damage(attack1, defense1)))
c = c + 2
Upvotes: 0
Views: 56
Reputation: 362097
for i in range(repeat): complete = random.randint(repeat,torandom) return complete
You want rolls between 1 and torandom
, not between repeat
and torandom
. You need to sum up all the rolls, so only return once the loop is finished.
complete = 0
for i in range(repeat):
complete += random.randint(1, torandom)
return complete
You could condense this to a one-liner using a generator expression, if you're comfortable with that feature.
return sum(random.randint(1, torandom) for i in range(repeat))
Upvotes: 2