Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8421037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:01:50+00:00 2026-06-10T03:01:50+00:00

I am learning Python from the book Exploring Python by Timothy Budd. One of

  • 0

I am learning Python from the book Exploring Python by Timothy Budd. One of the exercises from this chapter is this:

15. The function randint from the random module can be used to produce random numbers. A call on random.randint(1, 6), for example, will produce the values 1 to 6 with equal probability. Write a program that loops 1000 times. On each iteration it makes two calls on randint to simulate rolling a pair of dice. Compute the sum of the two dice, and record the number of times each value appears. Afterthe loop, print the array of sums. You can initialize the array using the idiom shown earlier in this chapter:

times = [0] * 12 # make an array of 12 elements, initially zero

I am able to print the sum in the array, but I have not understood the concept of recording the number of times each value appears. Also, what purpose would times = [0] serve? Here is my code for printing the sum:

 #############################################
#   Program to print the sum of dice rolls  #
#############################################

from random import randint
import sys


times = [0] * 12

summation = []

def diceroll():

    print "This program will print the"
    print "sum of numbers, which appears"
    print "after each time the dice is rolled."
    print "The program will be called 1000 times"



    for i in range(1,1000):

        num1 = randint(1,6)
        num2 = randint(1,6)

        sum = num1 + num2
        summation.append(sum)
        #times[i] = [i] * 12
    print summation
    #print times


diceroll()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:01:51+00:00Added an answer on June 10, 2026 at 3:01 am

    To count number of occurrences:

    times = [0]*13
    for _ in range(1000):
        sum_ = randint(1, 6) + randint(1, 6)
        times[sum_] += 1
    
    print(times[2:])
    

    Possible values of sum_ are 2…12 including. Possible times indexes are 0…12 including.

    times[i] corresponds to a number of occurrences of sum_ == i among 1000 tries.

    Note: times[0] and times[1] are always zero because sum_ > 1

    [x]*3 produces [x, x, x] It is a nicer version of:

    L = []
    L.append(x)
    L.append(x)
    L.append(x)
    

    Additional comments about your code:

    • print something is an error on Python 3
    • for i range(1, 1000) is wrong. It produces i from 1 to 999 including (999 iterations instead of 1000 as required). range doesn’t include upper limit as it customery for intervals in programming.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning Python from a book, and I came across this example: M =
I'm learning Python from a book and came across this example: >>> '%f, %.2f,
I'm currently learning python from a book called 'Python for the absolute beginner (third
I am learning from the book Python for Absolute Beginners and am up to
I am learning python atm, and was doing an exercise from this site --
I'm learning about Python Packages from Learn Python the Hard Way and one of
I'm learning Python and trying to test some rest web_service applications. From the below
I learning Python (coming from a dotnet background) and developing an app which interacts
I am learning Python by building a simple PyGTK application that fetches data from
I'm learning python recently, and is doing many practice with the language. One thing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.