You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

构建用户输入的4×4列表遇问题:VS Code无报错但代码不运行

4×4数字输入列表的问题修复

你的代码有两个关键问题导致无法运行:

  • 未初始化列表变量:直接调用List.append([])时,List这个变量还没被定义,Python会抛出NameError。必须在循环开始前先创建一个空列表:List = []。
  • 打印语句语法错误:最后循环里的print("List:" List)缺少逗号来分隔字符串和变量,而且循环拿到的每个子列表n没被使用,应该打印每个子列表,而不是重复输出整个大列表。

修正后的代码如下:

# 先初始化空列表
List = []
for i in range(4):
    List.append([])
    print(f"List {i} numbers ...")
    for j in range(4):
        number = int(input(f"Enter Numbers for the List: {j}: "))
        List[i].append(number)
        
print("\nList: \n")
for n in List:
    print(n)

内容的提问来源于stack exchange,提问作者Bioooz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 15:39:20