-1

I've encountered some problems when I was trying simulate school's math question,I've test the inner loop independently and the result is what I expect.I have no idea where is the problem and where can I get the resolution.It should be a simple bug.

This is the question: There is a bag which includes three red balls ,four white balls and five black balls.Take one ball each time.Then what is the probability when red balls were the first color being collected.

And This is my code:(All annotations were not added in my code)

import random as rd
y = 1000    *//total try*
succ = 0    *//success times*
orgbg = ['r','r','r','w','w','w','w','b','b','b','b','b']    *//original bag for each loop initialization*
while (y >= 0):
    redball = 0
    blackball = 0
    whiteball = 0
    newbg = orgbg    *//every bag for a single try*
    while (redball < 3 and whiteball < 4 and blackball < 5):
        tknum = rd.randrange(0,len(newbg),1)
        tkball = newbg[tknum]
        if (tkball == 'r'):
            redball = redball + 1
        elif (tkball =='w'):
            whiteball = whiteball + 1
        else:
            blackball = blackball + 1
        del newbg[tknum]
    if (redball == 3):
        succ = succ + 1
    y = y - 1
print (succ)

This is what the error report says: ValueError: empty range for randrange() (0,0, 0)

When I turn the code

tknum = rd.randrange(0,len(newbg),1)

into

tknum = rd.randrange(5,len(newbg),1)

The error reoprt says: ValueError: empty range for randrange() (5,5, 0)

I guess it is the initialization in the outer loop newbg = orgbg doesn't work out,but how can that happen?

Sorry for giving such a length question ,I'm a beginner and this is the first time I ask question on StackOverFlow,you can also give me some suggestion on my code style or method and the way of asking question,next time I will be better,hope you don't mind.

Kevin Lin
  • 3
  • 3
  • This is the exact code causing the error? Also, posting the exact error with stack trace in the question would help. The last hardcoded argument to `randrange` is changing from the code to the error message. – Carcigenicate Oct 04 '17 at 11:00
  • What is the stack trace? Can you post? – pstatix Oct 04 '17 at 11:15
  • The error says that your step size (third argument) is 0, but this doesn't match with your code. – noel Oct 04 '17 at 11:21
  • Traceback (most recent call last): File "C:/Users/KevinLin/PycharmProjects/Practice/test.py", line 11, in tknum = rd.randrange(0,len(newbg),1) File "C:\Users\KevinLin\AppData\Local\Programs\Python\Python36-32\lib\random.py", line 198, in randrange raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width)) ValueError: empty range for randrange() (0,0, 0) Process finished with exit code 1 Is this the thing that you're looking for? – Kevin Lin Oct 04 '17 at 13:12

1 Answers1

0

I think that your problem is indeed linked with the initialization in the outer loop newbg = orgbg. To correct your code, you should modify this line with

newbg = deepcopy(orgbg)

and import the corresponding module at the start of your code:

from copy import deepcopy

The explanation of the bug is a bit complicated and is linked with the way that Python handles the memory when copying a list. In fact, there is two possibility for this: a shallow or a deep copy. Here, you made a shallow copy when a deep copy would have been necessary. It is better explained here: https://www.python-course.eu/deep_copy.php or What exactly is the difference between shallow copy, deepcopy and normal assignment operation?

Dvg25
  • 98
  • 7