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
8 5992 -
for r in range(100):
-
d1 = diceroll()
-
d2 = diceroll()
-
sum = d1 + d2
-
print d1
-
print d2
-
print sum
-
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 -
-
sum=0
-
for i in range(100):
-
sum += 1
-
-
print sum
-
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... -
-
for r in range(100):
-
d1=diceroll()
-
d2=diceroll()
-
sum += (d1 + d2) # Add these two first, then add them to the sum
-
print d1
-
print d2
-
print sum
-
Hopefully, that makes sense :D
~shonen
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. - >>> import random
-
>>> diceLst = [1,2,3,4,5,6]
-
>>> roll = random.choice(diceLst), random.choice(diceLst)
-
>>> sum(roll)
-
6
-
>>> roll
-
(4, 2)
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
I like this die roller better: - from random import choice
-
die = [a for a in range(1, 7)]
-
print die
-
-
def roledie():
-
return choice(die)
-
-
-
print roledie()
-
Since this looks like homework, I will show you some hints toward a better solution. - >>> import random
-
>>> diceLst = [1,2,3,4,5,6]
-
>>> roll = random.choice(diceLst), random.choice(diceLst)
-
>>> sum(roll)
-
6
-
>>> roll
-
(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.
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.
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 -
>>> random.choice([1,2,3,'test'])
-
1
-
>>> random.choice([1,2,3,'test'])
-
'test'
-
>>>
-
however , in this die rolling case, randint() can also be used since what we want is integers.
I need to make a total sum variable for rolling two dice in python and am not sure how to do so. -
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
I need to make a total sum variable for rolling two dice in python and am not sure how to do so. -
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
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...": - total = 0
-
for r in range(100):
-
d1 = diceroll()
-
d2 = diceroll()
-
sum = d1 + d2
-
total += sum
-
print d1,
-
print d2,
-
print sum
-
print total
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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.
...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
| |