我正在尝试使用 Eclipse 在 python 3.0+ 中打印两个骰子可能掷出的二维列表 我有两个问题。首先,为什么我的教授给出了不带参数的函数,我应该为主程序编写什么? 其次,当它运行到 r.append(result) 时,它说 AttributeError: 'int' object has no attribute 'append' 有人可以帮助我吗,非常感谢 功能:
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]]
最佳答案
我不会做你的作业,而是回答你的问题:
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
考虑在整个脚本中更改类型时如何命名和重命名变量。
关于python - 在 python 中打印两个骰子可能掷出的二维列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38403168/