0
votes

I'm trying to make a dice rolling program in python that rolls dice given the users input on sides, dice, and rolls. Currently this code more or less works, but the problem I'm running into is say I give 3 dice being rolled 3 times with 6 sides to them.

The code I have displays this as the output:

Roll #1 6
Roll #2 5
Roll #3 1
Roll #4 6
Roll #5 4
Roll #6 6
Roll #7 3
Roll #8 1
Roll #9 1

When I need it to display as:

Roll #1 6 5 1
Roll #2 6 4 6
Roll #3 3 1 1

Here's my code so far. My guess is that it has to do something with my arguments and parameters being empty? I'm not entirely sure. Here is my code:

import random

def main ():
    rolls = get_rolls()
    dice = get_dice()
    sides = get_sides()

    nrolls = 1
    for r in range (rolls):
        for d in range (dice):
            print ('Roll #', nrolls, random.randint(1,sides))
            nrolls += 1 

def get_rolls():
    rolls = int(input('Enter the number of rolls: '))
    while rolls <= 0:
        print ('Number of rolls must be higher than 0')
        rolls = int (input('Enter the number of rolls: '))
    return rolls

def get_dice():
    dice = int (input('Enter the number of dice being rolled: '))
    while dice < 1 or 5 < dice:
        print ('Number of dice being rolled must be between 1 and 5')
        dice = int (input('Enter the number of dice being rolled: '))
    return dice ()

def get_sides():
    sides = int (input('Enter the number of sides on the dice: '))
    while sides < 2 or 36 < sides:
        print ('Number of sides on dice must be between 2 and 36')
        sides = int (input('Enter the number of sides on the dice: '))
    return sides


main()        
4

4 Answers

2
votes

Your problem is you put your print statement inside the inner for loop, which means you print the results too many times, as well as increment nrolls too often.

nrolls = 1
for r in range (rolls):
    roll = []
    for d in range (dice):
        roll.append(random.randint(1,sides))
    print('Roll #', nrolls, *roll)
    nrolls += 1
>Roll # 1 3 3 1
>Roll # 2 1 5 2
>Roll # 3 6 5 4
0
votes

For minimal modification to your code, change it to this:

for r in range (rolls):
    print('Roll #', nrolls, end=' ')
    for d in range (dice):
        print (random.randint(1,sides), end=' ')
    print()
    nrolls += 1

Additionally, the following line throws an error.

return dice ()

Change it to:

return dice
0
votes
import random

def dice_roll(n):
dicelist = ['\u2680','\u2681','\u2682','\u2683','\u2684','\u2685']
dicevalue = {(dicelist[0]): 1, (dicelist[1]): 2, (dicelist[2]): 3, 
(dicelist[3]): 4, (dicelist[4]): 5,(dicelist[5]): 6}
result = 0
visualresult = " "

for i in range(n): 
    random_roll = random.sample((dicelist), 1)
    str1 = str(random_roll)
    str2 = str1.replace("'"," ")
    str3 = str2.replace("[", " ")
    str4 = str3.replace(']',' ')
    str5 = str4.strip()
    valueresultindex = dicelist.index(str5)
    valuelist = list(dicevalue.values())
    newvalueindex = valuelist[valueresultindex]
    result = result + newvalueindex
    visualresult = visualresult + str4
    i = i + 1

rolledstring = "You rolled this much: "    
print(visualresult)
print((rolledstring) + str(result))

Numofdice = int(input("Please enter the amount of dice you would like to 
roll: "))

if Numofdice >= 1 and Numofdice <= 6:
    ValidRollAmount = True
else:
    ValidRollAmount = False

while ValidRollAmount == False:
    Numofdice = int(input("Invalid amount of dice please re-enter: "))    
    if Numofdice >= 1 and Numofdice <= 6:
        ValidRollAmount = True

while ValidRollAmount == True:
    dice_roll(Numofdice)
    ValidRollAmount = False
0
votes

You should use a while loop for continuing the asking for dice then rolling that amount of dice.

e.g.

import random
from random import randint
import string

while True:
  str = input("What dice would you like to roll")
  prefix, d, suffix = str.partition("d")
  if str == "quit" or str == "q":
    break
  elif str.startswith("d"):
    print random.randint(1, int(suffix))
  elif str.startswith(prefix):
    for _ in range(int(prefix)):
      print (random.randint(1, int(suffix)))
  elif str.startswith("d"):
    print random.randint(1, suffix)
  else:
   print ("Keep rolling or type q or quit")

print ("exiting")