我正在尝试使用Eclipse打印python 3.0+中两个骰子的可能卷的2D列表 我有两个问题。首先,为什么我的教授给出了不带参数的函数,我应该为主程序写什么? 其次,当它运行到r.append(结果)时,它说AttributeError:' int'对象没有属性'追加' 请有人帮助我,非常感谢你 功能:
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()
结果应如下所示
两个骰子卷表
[[2 3 4 5 6 7] ............. [7 8 9 10 11 12]]
答案 0 :(得分:1)
我不会做你的作业,而是回答你的问题:
1)可能是因为你的教授希望让作业更具挑战性
2)因为当你试图追加r
时,r
是一个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
在整个脚本中更改类型时,请考虑如何命名和重命名变量。