python - 在 python 中打印两个骰子可能掷出的二维列表

标签 python

我正在尝试使用 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/

相关文章:

python - 加载 scipy : OSError: [WinError 126] The specified module could not be found 时出错

printing - Python 解释器中的 `>>> some_object` 和 `>>> print some_object` 有什么区别?

python - 属性错误:模块 'streamlit' 没有属性 'chat_input'

python - pyodbc rowcount 返回负数

python - 属性错误 : module 'matplotlib.pyplot' has no attribute 'axline'

python - 如果qtableview中的单元格不为空,如何为单元格设置颜色?

python - SLURM srun 不并行运行 python 脚本,但可以访问并行资源

Python3 'Cannot import name ' cached_property'

Python如何在不使用 super 用户权限的情况下创建pycache文件夹?

python - ubuntu 与 windows 中通过 python 的屏幕截图