I don't get what's wrong with this code
I keep getting a value error when i run this
it says

Traceback (most recent call last):
File "rolling.py", line 40, in <module>

``````    dice1, dice2, dice3 = numb(dice)
``````

ValueError: need more than 2 values to unpack

``````def numb(dice):

if dice == 1:

dice1 = random.randint(1, 6)

return dice1

elif dice == 2:

dice1 = random.randint(1, 6)

dice2 = random.randint(1, 6)

return dice1, dice2

elif dice == 3:

dice1 = random.randint(1, 6)

dice2 = random.randint(1, 6)

dice3 = random.randint(1, 6)

return dice1, dice2, dice3

else:

print "Invalid"

dice1, dice2, dice3 = numb()

if dice == 1:

print "You got " + str(dice1)

elif dice == 2:

if dice1 == dice2 :

print "2 matches"

else:

print "0 matches"

print "You got " + str(dice1) +  " and " + str(dice2)

print "Sum is " + str(dice1 + dice2)

elif dice == 3:

if dice1 == dice2 == dice3 :

print "3 matches"

elif dice1 == dice2 :

print "2 matches"

elif dice2 == dice3 :

print "2 matches"

elif dice1 == dice3 :

print "2 matches"

else:

print "0 matches"

print "You got " + str(dice1) +  " and " + str(dice2) + " and " + str(dice3)

print "Sum is " + str(dice1 + dice2 + dice3)
``````
3
Contributors
2
Replies
18
Views
5 Years
Discussion Span
Last Post by woooee

The line `dice1, dice2, dice3 = numb(dice)` does not appear in the code you've posted, so I think you accidentally posted the wrong code. Assuming that the definition of `numb` that you gave in the code you've posted is the same one that you use in your real code and assuming that everything up to line 30 is supposed to be indented, the error message that you quoted would be caused if the value of `dice` is something other than 3.

Your code can return one, two, or three values. The error occurs when less (or more) than three vaules are returned (i.e. dice1, dice2, dice3 and only two are returned from the function). Usually you would catch the return with a tuple Click Here since you want to catch any number of dice returned.

``````dice1, dice2, dice3 = numb(dice)
## becomes
dice_returned=numb(dice)
print len(dice_returned), "created"
## and to print or use individually
for dy in dice_returned:
print dy
``````

Also, you can use a for() loop in the function

``````def numb(dice):
return_list=[]
for ctr in range(dice):
dy = random.randint(1, 6)
return_list.append(dy)
return return_list
``````

Edited by woooee

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.