Reputation: 49
I am trying to print a 2D list of the possible rolls of two dice in python 3.0+, using Eclipse I have two questions. First, why my prof gave the function that take no arguments, what should I write for the main program? Second, when it runs to r.append(result) , it said AttributeError: 'int' object has no attribute 'append' Can anyone helps me please, Thank you so much functions:
def roll_two_dice():
"""
-------------------------------------------------------
Prints a 2D list of the possible rolls of two dice.
-------------------------------------------------------
Postconditions:
prints
a table of possible dice rolls
-------------------------------------------------------
"""
r = []
rolls = []
for r in range(DICE1):
for c in range(DICE2):
result = r + c + 1
r.append(result)
rolls.append(r)
r = []
print("{}".format(rolls))
main program
from functions import roll_two_dice
roll_two_dice()
the result should look like this
Table of Two Dice Rolls
[[2 3 4 5 6 7] ............. [7 8 9 10 11 12]]
Upvotes: 1
Views: 197
Reputation: 794
I am not going to do your homework, but to answer your questions:
1) Probably because your Professor wanted to make the assignment more challenging
2) Because when you are trying to append r
, r
is an int
for r in range(DICE1): # r are the integers in the range of the 1st die
for c in range(DICE2):
result = r + c + 1
r.append(result) # you are trying to append result to said integer
Think about how you name and rename your variables as you change the types throughout the script.
Upvotes: 1