# Trying to Make a DND Style Dice Roller

I am trying to get a dice roller program that works for DND that you would enter number of dice, what sided dice and a modifier to add to the result. I had gotten it able to do just 1 dice without a problem but cannot seem to get it to work right for multiple dice. With the code as is, typing 7 doesn’t not exit the program as it should, when i print an answer, I get however many numbers of dice I chose lines. So, if I put 3 for number of dice, it is giving me the result of each dice plus modifier, not the sum of the dice + modifier in one line as I want.

I tried 2 ways:
way 1:
import random
run = True

print("""
Enter 1 for a D20
Enter 2 for a D12
Enter 3 for a D10
Enter 4 for a D8
Enter 5 for a D6
Enter 6 for a D4
Enter 7 to Exit
“”")

while run and end < num_dice:
number = input("Enter number of choice here: ")
num_dice = int(input("Enter number of dice being rolled: "))
modifier = int(input(“How much to modify the roll by?”))
end = 0
total = 0

``````if number == "1":
roll = int(random.randint(1, 20))
total += roll
outcome = total + modifier
print("Dice roll: " + str(total) + "+" + str(modifier)+ " = " + str(outcome))
end += 1

elif number == "2":
roll = int(random.randint(1, 12))
total += roll
outcome = total + modifier
print("Dice roll: " + str(total) + "+" + str(modifier) + " = " + str(outcome))
end += 1

elif number == "3":
roll = int(random.randint(1, 10))
total += roll
outcome = total + modifier
print("Dice roll: " + str(total) + "+" + str(modifier) + " = " + str(outcome))
end += 1

elif number == "4":
total += roll
outcome = total + modifier
print("Dice roll: " + str(total) + "+" + str(modifier) + " = " + str(outcome))
end += 1

elif number == "5":
total += roll
outcome = total + modifier
print("Dice roll: " + str(total) + "+" + str(modifier) + " = " + str(outcome))
end += 1

elif number == "6":
total += roll
outcome = total + modifier
print("Dice roll: " + str(total) + "+" + str(modifier) + " = " + str(outcome))
end += 1

elif number == "7":
run = False
``````

way 2) (with same results)
import random
run = True

print("""
Enter 1 for a D20
Enter 2 for a D12
Enter 3 for a D10
Enter 4 for a D8
Enter 5 for a D6
Enter 6 for a D4
Enter 7 to Exit
“”")

while run:
number = input("Enter number of choice here: ")
num_dice = int(input("Enter number of dice being rolled: "))
modifier = int(input(“How much to modify the roll by?”))
end = 0
total = 0
while end < num_dice:
if number == “1”:
roll = int(random.randint(1, 20))
total += roll
outcome = total + modifier
print("Dice roll: " + str(total) + “+” + str(modifier)+ " = " + str(outcome))
end += 1

``````    elif number == "2":
total += roll
outcome = total + modifier
print("Dice roll: " + str(total) + "+" + str(modifier) + " = " + str(outcome))
end += 1

elif number == "3":
total += roll
outcome = total + modifier
print("Dice roll: " + str(total) + "+" + str(modifier) + " = " + str(outcome))
end += 1

elif number == "4":
total += roll
outcome = total + modifier
print("Dice roll: " + str(total) + "+" + str(modifier) + " = " + str(outcome))
end += 1

elif number == "5":
total += roll
outcome = total + modifier
print("Dice roll: " + str(total) + "+" + str(modifier) + " = " + str(outcome))
end += 1

elif number == "6":
total += roll
outcome = total + modifier
print("Dice roll: " + str(total) + "+" + str(modifier) + " = " + str(outcome))
end += 1

elif number == "7":
run = False
``````

Anyone know how i can get this to 1) have 7 still exit the program as it should as well as just give me 1 answer instead of number of dices rolled answers?

Below is what i mean by number of dices rolled answers:
Enter number of choice here: 1
Enter number of dice being rolled: 2
How much to modify the roll by?2
Dice roll: 15+2 = 17
Dice roll: 22+2 = 24

for way 2, this is what the coding looks like. When i pasted it in the post, ite didnt recognize it all as code.

You can use markdown to format posts. To make a code section, put three backticks on the line above the code, and three backticks on the line below the code. Try it out here.

That way your code will look like code

``````this = code;
``````

and it will be easier for other people to copy/paste and work with.

``````import random
run = True

print("""
Enter 1 for a D20
Enter 2 for a D12
Enter 3 for a D10
Enter 4 for a D8
Enter 5 for a D6
Enter 6 for a D4
Enter 7 to Exit
""")

while run:
number = input("Enter number of choice here: ")
num_dice = int(input("Enter number of dice being rolled: "))
modifier = int(input("How much to modify the roll by?"))
end = 0
total = 0
while end < num_dice:
if number == "1":
roll = int(random.randint(1, 20))
total += roll
outcome = total + modifier
print("Dice roll: " + str(total) + "+" + str(modifier)+ " = " + str(outcome))
end += 1

elif number == "2":
total += roll
outcome = total + modifier
print("Dice roll: " + str(total) + "+" + str(modifier) + " = " + str(outcome))
end += 1

elif number == "3":
total += roll
outcome = total + modifier
print("Dice roll: " + str(total) + "+" + str(modifier) + " = " + str(outcome))
end += 1

elif number == "4":
total += roll
outcome = total + modifier
print("Dice roll: " + str(total) + "+" + str(modifier) + " = " + str(outcome))
end += 1

elif number == "5":
total += roll
outcome = total + modifier
print("Dice roll: " + str(total) + "+" + str(modifier) + " = " + str(outcome))
end += 1

elif number == "6":
total += roll
outcome = total + modifier
print("Dice roll: " + str(total) + "+" + str(modifier) + " = " + str(outcome))
end += 1

elif number == "7":
run = False
``````

`"7"` isn’t exiting because, even though you set `run` to `False`, you can get stuck in the `while end < num_dice` loop forever when `end` is less than `num_dice`. A possible solution is to also check for `run` in that loop.

``````while end < num_dice and run:
``````

Or better yet, don’t even ask the user for the number of dice or the modifier if we already know that they want to exit.

As for the printing multiple line thing, what you could do is move these two lines

``````outcome = total + modifier
print("Dice roll: " + str(total) + "+" + str(modifier)+ " = " + str(outcome))
``````

outside of your inner loop, so that your inner loop is just calculating the total of the random dice rolls, and then after all of that you add the modifier and print the result.

I ended up getting it to work. Just so it is here for anyone else that might want to know:

``````import random
run = True

print("""
Enter 1 for a D20
Enter 2 for a D12
Enter 3 for a D10
Enter 4 for a D8
Enter 5 for a D6
Enter 6 for a D4
Enter 7 to Exit
""")

while run:
number = input("Enter number of choice here: ")
num_dice = int(input("Enter number of dice being rolled: "))
modifier = int(input("How much to modify the roll by?"))
end = 0
total = 0
while end < num_dice:
if number == "1":
roll = int(random.randint(1, 20))
total += roll
outcome = total + modifier

elif number == "2":
roll = int(random.randint(1, 12))
total += roll
outcome = total + modifier

elif number == "3":
roll = int(random.randint(1, 10))
total += roll
outcome = total + modifier

elif number == "4":
roll = int(random.randint(1, 8))
total += roll
outcome = total + modifier

elif number == "5":
roll = int(random.randint(1, 6))
total += roll
outcome = total + modifier

elif number == "6":
roll = int(random.randint(1, 4))
total += roll
outcome = total + modifier

end +=1
if number != "7":
print("Dice roll: " + str(total) + "+" + str(modifier)+ " = " + str(outcome))
else:
run = False
``````

Did you notice all that code duplication? Consider asking for the number of sides of the dices and using that as the second argument to `randint()`. That way you’re more flexible and can get rid of all the `if`s and the duplicated code blocks for rolling and calculating the total.

And BTW: Shouldn’t the modifier be added to the total once instead of per dice?

Yeah im new to coding. So im sure there is more condensed ways to do the code. Ill have to look. I wasnt thinking the modifier was being done twice.