擅长:python、mysql、java
<p>你的代码有几个问题。你知道吗</p>
<p>当你读用户输入时,你会得到字符串。如果你想用这个输入做算术运算,你必须把它转换成数字。你知道吗</p>
<p>您的代码有点混乱,因为您使用<code>userInput</code>作为初始用户输入,但是您尝试将其用于正在构建的数字网格。你知道吗</p>
<p>您试图在<code>userInput[x][y]</code>存在之前设置它,这样就行不通了。使用Python列表的通常方法是在列表末尾添加内容。你知道吗</p>
<p>这是您的代码的修改版本。你知道吗</p>
<pre><code>from __future__ import print_function
user_input = raw_input("Enter values for row and column number: ")
rows, cols = user_input.split(",")
rows = int(rows)
cols = int(cols)
grid = []
for x in range(rows):
row = []
for y in range(cols):
row.append(x * y)
grid.append(row)
print(row)
print()
print(grid)
</code></pre>
<p>演示</p>
<pre><code>Enter values for row and column number: 4,5
[0, 0, 0, 0, 0]
[0, 1, 2, 3, 4]
[0, 2, 4, 6, 8]
[0, 3, 6, 9, 12]
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8], [0, 3, 6, 9, 12]]
</code></pre>
<p>顺便说一句,既然您刚刚开始学习Python,那么您应该学习python3。如果你真的需要的话,你可以回头学习老式的python2。你知道吗</p>