Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random Dice roll program
#1
Hello,

Goal: Create a simple program that recreates a table-top game mechanic; rolling dice with a varying number of sides.

Python: Using 3.5.2

Problem: The code pasted below seems to work fine, on the surface. However, if I run it using an input of "2d2", it will only ever produce a result of 3 or 4. I'm worried I'm missing something that is preventing the random generator from using the lowest number (1).

Code:
# Allow a user to input a dice in the classic '2d6' format
# (which would indicate 2 dice with 6 sides) and create the right random dice roll
import random

while True:
    roll = input("What do you want to roll?")
    if roll == "exit":
        break
    else:
        # split the string at d. So '2d6' becomes ['2', '6']
        splitRoll = roll.split("d")
        # convert the string to an integer. so ['2', '6'] becomes [2,6]
        intRoll = [int(i) for i in splitRoll]
        # set (or reset) result to 0
        result = 0
        # create random numbers for each dice rolled and add them together
        for i in range(intRoll[0]):
            result += random.randint(1, intRoll[1])
        print (result)
        result = 0
Reply
#2
Prints a 2 for me (eventually).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Noob here. Random quiz program. Using a while loop function and alot of conditionals. monkeydesu 6 1,405 Sep-07-2022, 02:01 AM
Last Post: kaega2
  Program for random sum Cristopher 7 1,659 Jan-15-2022, 07:38 PM
Last Post: Cristopher
  Dice Roll (Find out how many rolls until specified streak) DustinKlent 4 4,010 Jun-13-2021, 09:44 AM
Last Post: Gribouillis
  Help with dice roll program kraco 4 2,112 Sep-22-2020, 02:06 PM
Last Post: kraco
  simple dice roll Byzas 1 2,355 Mar-21-2019, 02:29 AM
Last Post: ichabod801
Photo roll of the dice kyle007 0 1,731 Mar-11-2019, 01:58 AM
Last Post: kyle007
  unit test roll die saladgg 5 4,188 Nov-06-2018, 11:39 PM
Last Post: stullis
  Issue with my 'roll the dice simulation'-exercise (cannot break out of the loop) Placebo 2 3,517 Sep-30-2018, 01:19 PM
Last Post: Placebo
  Making a percentile dice roller and dice roller Fixer243 2 3,247 Sep-30-2018, 12:18 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020