473,809 Members | 2,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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 6040
shonen
7 New Member
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 Recognized Expert Moderator Specialist
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 Recognized Expert Expert
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 Recognized Expert Expert
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 Recognized Expert New Member
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 Recognized Expert Contributor
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
BillMeehan
2 New Member
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 Recognized Expert Expert
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
11830
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 is make a self totalling version of this page > http://www.ayrshirehousing.org.uk/applications/applications.html so that basically as a person checked or unchecked boxes, a
0
749
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. #include <iostream> #include <fstream>//for declaring objects of the ofstream and ifstream #include <time.h> #include <stdlib.h> using namespace std;
7
12094
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 to know this number after the first FETCH, isn't it? On a side note, why queries using LIMIT are SO terribly slow, compared to cursors and sometimes even ones without LIMIT? Shouldn't LIMIT be internally implemented using cursor mechanism then?...
10
2173
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 second. What would cause this? Is it some kind of postgresql.conf configuration failure? I have the same query running fine on a different machine. QUERY PLAN...
3
3845
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 anyone point me in the right direction? Thanks! # include <iostream> # include <cmath> using namespace std; int i; //int sqrt;
4
2083
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
1920
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 button. I then want to use the date i entered into the cell to sum the total of numbers from worksheet 2 by filtering the numbers by the date. I want to enter 7/15/2007. I want to return the total count of all "Reg" jobs somewhere on worksheet 1 ...
2
4084
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 value is either true (1) or false(0). So create an int and set it to false before the inner loop starts, then if a swap is made set it to true. After the inner loop finishes check the value of that int and if it is true then break out of the outer...
4
1425
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 you can see the Joint Honours combinations. As not all Subject combinations are possible there are many null fields. For example Geography with Geography. With a rather large IIf statement in a text box I can create a row total, but I want an...
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10637
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10379
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10115
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3014
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.