Python homework

ssghotra1997 at gmail.com ssghotra1997 at gmail.com
Wed Dec 6 20:18:10 EST 2017


On Wednesday, December 6, 2017 at 9:28:26 PM UTC+11, D'Arcy Cain wrote:
> On 12/05/2017 07:33 PM, nick.martinez2--- via Python-list wrote:
> > I have a question on my homework. My homework is to write a program in which the computer simulates the rolling of a die 50
> > times and then prints
> > (i). the most frequent side of the die
> > (ii). the average die value of all rolls.
> > I wrote the program so it says the most frequent number out of all the rolls for example (12,4,6,14,10,4) and will print out "14" instead of 4 like I need.
> 
> How did you come up with 4?  I come up with 3.36 with those numbers.
> 
> > This is what I have so far:
> > import random
> > 
> > def rollDie(number):
> >      rolls = [0] * 6
> 
> For a small efficiency use "[0] * 7".  See below for reason.
> 
> >      for i in range(0, number):
> >          roll=int(random.randint(1,6))
> >          rolls[roll - 1] += 1
> 
> Use "rolls[roll] += 1" here.  You save one arithmetic instruction each 
> time through the loop.  Fifty times isn't much saving but imagine fifty 
> million.
> 
> >      return rolls
> 
> return rolls[1:].  No matter how many rolls you only need to do the 
> slice once.  However, you really need a lot of iterations before it 
> really affects the total run time.
> 
> You need one more thing though.  Create a new variable called "total" 
> and set it to 0.0.  Add "total += roll" to your loop.  your return 
> statement is now "return rolls[1:], total/number".
> 
> > 
> > if __name__ == "__main__":
> >      result = rollDie(50)
> 
> Now "result, average = ..."
> 
> >      print (result)
> >      print(max(result))
> 
> This is why you get 14.  The maximum number of rolls for any one side is 
> 14 for side 4.  Is that where you got "4"?
> 
> -- 
> D'Arcy J.M. Cain
> Vybe Networks Inc.
> http://www.VybeNetworks.com/
> IM:darcy at Vex.Net VoIP: sip:darcy at VybeNetworks.com

The 'four' is the number which was rolled the highest during the 50 rolls



More information about the Python-list mailing list