例子:
def main():
names = ['关羽', '张飞', '赵云', '马超', '黄忠']
subjs = ['语文', '数学', '英语']
scores = [[0] * 3] * 5
for row, name in enumerate(names):
print('请输入%s的成绩' % name)
for col, subj in enumerate(subjs):
scores[row][col] = float(input(subj + ': '))
print(scores)
if __name__ == '__main__':
main()
图1修改后:
def main():
names = ['关羽', '张飞', '赵云', '马超', '黄忠']
subjs = ['语文', '数学', '英语']
scores = [[]] *5
for row, namein enumerate(names):
print('请输入%s的成绩' % name)
scores[row] = [0] *3
for col, subjin enumerate(subjs):
scores[row][col] =float(input(subj +': '))
print(scores)
if __name__ =='__main__':
main()
图2上面的两个图来源于http://www.pythontutor.com/visualize.html#mode=edit,可视化执行功能
看图1我们可以猜出运行的结果,返回的都是最后输入的一组数据
网友评论