473,320 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How do I get a total sum using a for loop of 100 numbers?

I need to use a for loop with the range function to roll the dice 100 times and have a total sum as well as a sum of 2 dice rolls that starts at 0 and shows the total sum after all 100 rolls. I have this so far:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. def diceroll():
  4.     r = random.random()
  5.  
  6.     if r < 1.0/6:
  7.         r = 1
  8.     elif r < 2.0/6:
  9.         r = 2
  10.     elif r < 3.0/6:
  11.         r = 3
  12.     elif r < 4.0/6:
  13.         r = 4
  14.     elif r < 5.0/6:
  15.         r = 5
  16.     else:
  17.         r = 6
  18.  
  19.     return r
  20.  
  21. for r in range(100):
  22.     d1 = diceroll()
  23.     d2 = diceroll()
  24.     sum = d1 + d2
  25.     print d1,
  26.     print d2,
  27.     print sum
Mar 27 '07 #1
8 5992
shonen
7
Expand|Select|Wrap|Line Numbers
  1. for r in range(100):
  2.     d1 = diceroll()
  3.     d2 = diceroll()
  4.     sum = d1 + d2
  5.     print d1
  6.     print d2
  7.     print sum
  8.  
Ok well let's check out your code here, (cause this is where you need to check it)

you're calling your diceroll 100 times BUT because python overwrites your variables, you're basically overwriting your variable sum 100 times and then printing it out. (we've all made this mistake)

now what i did just now on my linux system was this

Expand|Select|Wrap|Line Numbers
  1.  
  2. sum=0
  3. for i in range(100):
  4.      sum += 1
  5.  
  6. print sum
  7.  
so this made sum 100(counted from 0 to 100). See you have to add to your variable as you go along...not just set it equal to. so for you...

Expand|Select|Wrap|Line Numbers
  1.  
  2. for r in range(100):
  3.      d1=diceroll()
  4.      d2=diceroll()
  5.      sum += (d1 + d2) # Add these two first, then add them to the sum
  6.      print d1
  7.      print d2
  8.      print sum
  9.  
Hopefully, that makes sense :D

~shonen
Mar 27 '07 #2
bvdet
2,851 Expert Mod 2GB
I need to use a for loop with the range function to roll the dice 100 times and have a total sum as well as a sum of 2 dice rolls that starts at 0 and shows the total sum after all 100 rolls. I have this so far:

import random

def diceroll():
r = random.random()

if r < 1.0/6:
r = 1
elif r < 2.0/6:
r = 2
elif r < 3.0/6:
r = 3
elif r < 4.0/6:
r = 4
elif r < 5.0/6:
r = 5
else:
r = 6

return r

for r in range(100):
d1 = diceroll()
d2 = diceroll()
sum = d1 + d2
print d1,
print d2,
print sum
Since this looks like homework, I will show you some hints toward a better solution.
Expand|Select|Wrap|Line Numbers
  1. >>> import random
  2. >>> diceLst = [1,2,3,4,5,6]
  3. >>> roll = random.choice(diceLst), random.choice(diceLst)
  4. >>> sum(roll)
  5. 6
  6. >>> roll
  7. (4, 2)
Mar 27 '07 #3
bartonc
6,596 Expert 4TB
I need to use a for loop with the range function to roll the dice 100 times and have a total sum as well as a sum of 2 dice rolls that starts at 0 and shows the total sum after all 100 rolls. I have this so far:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. def diceroll():
  4.     r = random.random()
  5.  
  6.     if r < 1.0/6:
  7.         r = 1
  8.     elif r < 2.0/6:
  9.         r = 2
  10.     elif r < 3.0/6:
  11.         r = 3
  12.     elif r < 4.0/6:
  13.         r = 4
  14.     elif r < 5.0/6:
  15.         r = 5
  16.     else:
  17.         r = 6
  18.  
  19.     return r
  20.  
  21. for r in range(100):
  22.     d1 = diceroll()
  23.     d2 = diceroll()
  24.     sum = d1 + d2
  25.     print d1,
  26.     print d2,
  27.     print sum
I like this die roller better:
Expand|Select|Wrap|Line Numbers
  1. from random import choice
  2. die = [a for a in range(1, 7)]
  3. print die
  4.  
  5. def roledie():
  6.     return choice(die)
  7.  
  8.  
  9. print roledie()
  10.  
Mar 27 '07 #4
bartonc
6,596 Expert 4TB
Since this looks like homework, I will show you some hints toward a better solution.
Expand|Select|Wrap|Line Numbers
  1. >>> import random
  2. >>> diceLst = [1,2,3,4,5,6]
  3. >>> roll = random.choice(diceLst), random.choice(diceLst)
  4. >>> sum(roll)
  5. 6
  6. >>> roll
  7. (4, 2)
Sorry, BV. I was composing while you were posting. Didn't mean to step on your hints.
Thank you for being aware of Posting Guidelines re. homework. And thank you so much for all the time you give to our members.
Mar 27 '07 #5
dshimer
136 Expert 100+
This is off subject, but wouldn't random.randint(1,6) be the easiest way to get a die? I use randint() on occaision and when it didn't get mentioned it made me wonder if there are issues.
Mar 28 '07 #6
ghostdog74
511 Expert 256MB
This is off subject, but wouldn't random.randint(1,6) be the easiest way to get a die? I use randint() on occaision and when it didn't get mentioned it made me wonder if there are issues.
there's a difference. you can use randint only for integers. choice() is used on a sequence, where a sequence can be a mix of data types something like this
Expand|Select|Wrap|Line Numbers
  1. >>> random.choice([1,2,3,'test'])
  2. 1
  3. >>> random.choice([1,2,3,'test'])
  4. 'test'
  5. >>>
  6.  
however , in this die rolling case, randint() can also be used since what we want is integers.
Mar 28 '07 #7
I need to make a total sum variable for rolling two dice in python and am not sure how to do so.
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. def diceroll():
  4.     r = random.random()
  5.  
  6.     if r < 1.0/6:
  7.         r = 1
  8.     elif r < 2.0/6:
  9.         r = 2
  10.     elif r < 3.0/6:
  11.         r = 3
  12.     elif r < 4.0/6:
  13.         r = 4
  14.     elif r < 5.0/6:
  15.         r = 5
  16.     else:
  17.         r = 6
  18.  
  19.     return r
  20.  
  21. for r in range(100):
  22.     d1 = diceroll()
  23.     d2 = diceroll()
  24.     sum = d1 + d2
  25.     print d1,
  26.     print d2,
  27.     print sum
Mar 28 '07 #8
bartonc
6,596 Expert 4TB
I need to make a total sum variable for rolling two dice in python and am not sure how to do so.
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. def diceroll():
  4.     r = random.random()
  5.  
  6.     if r < 1.0/6:
  7.         r = 1
  8.     elif r < 2.0/6:
  9.         r = 2
  10.     elif r < 3.0/6:
  11.         r = 3
  12.     elif r < 4.0/6:
  13.         r = 4
  14.     elif r < 5.0/6:
  15.         r = 5
  16.     else:
  17.         r = 6
  18.  
  19.     return r
  20.  
  21. for r in range(100):
  22.     d1 = diceroll()
  23.     d2 = diceroll()
  24.     sum = d1 + d2
  25.     print d1,
  26.     print d2,
  27.     print sum
I have merged you posts which are on a single topic and added CODE tags to your recent post. There are Posting Guidelines all over the place, so that you may learn proper forum posting prectices. Please take some time to read them.
That said, in reply to "I can't understand...":
Expand|Select|Wrap|Line Numbers
  1. total = 0
  2. for r in range(100):
  3.     d1 = diceroll()
  4.     d2 = diceroll()
  5.     sum = d1 + d2
  6.     total += sum
  7.     print d1,
  8.     print d2,
  9.     print sum
  10. print total
Mar 28 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: James Greig | last post by:
hello people, i'm just learning javascript, could someone point me in the direction of an example of the following, or give me some clues as to how it might be done: what i would like to do...
0
by: August1 | last post by:
This is a follow up to using these functions to produce lottery numbers to an outfile, then read the numbers from the file to the screen. Although other methods are certainly available. ...
7
by: Egor Shipovalov | last post by:
I'm implementing paging through search results using cursors. Is there a better way to know total number of rows under a cursor than running a separate COUNT(*) query? I think PostgreSQL is bound...
10
by: Greg Stark | last post by:
This query is odd, it seems to be taking over a second according to my log_duration logs and according to psql's \timing numbers. However explain analyze says it's running in about a third of a...
3
by: triplejump24 | last post by:
Hey. Im trying to make program that basically displays all the prime numbers. I need to use bool and for but im not quite sure if i have this right. So far i have this bunch of a mess but can...
4
by: GeekBoy | last post by:
I am reading a file of numbers using for loops. The numbers are in a grid as follows: 8 36 14 11 31 17 22 23 17 8 9 33 23 32 18 39 23 25 9 38 14 38 4 22 18 11 31 19 16 17 9 32 25 8 1 23
1
by: crystalgal | last post by:
Help. I am using Excel to enter in daily numbers in worksheet 2. I want to use vb (w/in excel) to create a command button in worksheet 1. I want to enter a date in a cell and click the command...
2
by: Villanmac | last post by:
Okay basically I have built the code using the pseudocode given to me but i still have on psedocode to mark off and I dont know how to do what it says. Its says "a flag is just an integer whose...
4
by: Mysterydave | last post by:
Hi, Here is my problem: I have a report which lists Application numbers, Offer numbers and Firm Replies numbers for a subject i.e. Geography as 3 rows. My column headings also list subjects so...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.