1

I am an amateur programmer who is working on a console dice roller, quite more complex than the traditional ones. So far I am having trouble with this function:

  def multiRoll(dice, amount):
        total = 0
        rolls = []
    for roll in range(amount):
        rolls += roll(dice)
    total = sum(rolls)
    return total, rolls

It is meant to take a single entry from a dictionary describing the kind of dice (4, 6, 8 faces, etc.) with its index and the times the dice is rolled. Then it rolls the dice as much as it is needed and then it is meant to add each result into a single list that will be summed later.

But apparently the flux goes wrong and when it reaches the this line:

        rolls += roll(dice)

I get 'int' object is not callable.

May you please tell me how to overcome this?

If you have any doubt about the roll() function, here they are:

def roll(dice):
    if dice < 1:
            print "I found a problem when trying to roll a d%d" % (dice)
            return "Failed"
    else:
            return randrange(1, dice)
gcarbonell
  • 33
  • 4

3 Answers3

1
for roll in range(amount):
        rolls += roll(dice)

The roll(dice) isn't using your function, but rather the roll variable of your for loop. You should change the variable of the loop to be:

for number in range(amount):
    rolls += roll(dice) 

Or something like that.

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
Name McChange
  • 2,750
  • 5
  • 27
  • 46
0

You have reassigned roll in

for roll in range(amount):
        rolls += roll(dice)

Replace it with another variable name:

for r in range(amount):
        rolls += roll(dice)
phihag
  • 278,196
  • 72
  • 453
  • 469
  • On a side note, the underscore(`_`) is usually used for such "junk" variables. Or you can name it with something more explicit like "junk", "not_used", or in this case "time"(recalling "for x time(s)"). [Note: if you use `gettext` using `_` may not be a good idea] – Bakuriu Nov 15 '12 at 21:59
  • Thank you very much; you guys were incredibly quick :D – gcarbonell Nov 15 '12 at 22:03
  • @Bakuriu Yup, but the underscore [tends to confuse programmers new to Python](http://stackoverflow.com/questions/5893163/underscore-in-python), so I left it out – phihag Nov 15 '12 at 22:04
0

In this line here:

for roll in range(amount):
    rolls += roll(dice)

You are creating a local variable called roll which is overwriting the function roll which you have defined in your program.
What you need to do is change either the function name or the local variable name so that they are not the same.

Serdalis
  • 10,296
  • 2
  • 38
  • 58