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

Python嵌套字典问题:创建含200个子字典的总字典显示异常

问题排查与修复方案

Hey, I see the issue with your code—let's break down why only the latest student entry shows up when you try to create 200 of them, and fix it:

核心问题

  1. You didn't initialize the totaldict dictionary
    You're trying to add entries to totaldict but never created it first. If this was a global variable, it might get overwritten unexpectedly; if not, your code should throw an error (unless you accidentally defined it elsewhere, which would cause messy logic).
  2. Your loop only runs twice
    range(1,3) generates numbers 1 and 2, so you're only creating 2 student entries, not 200. You need to adjust this range to cover 200 iterations.
  3. Redundant code line
    The line studno=('student'+str(count)) does nothing—you immediately overwrite studno with the student dictionary right after, so you can delete it entirely.

Fixed Code

def individualstudent():
    # Initialize the total dictionary inside the function to avoid global variable issues
    totaldict = {}
    # Loop from 1 to 201 to create 200 student entries
    for count in range(1, 201):
        sname = input(f'Enter name for student {count} : ')
        sID = int(input(f'Enter ID for student {count} : '))
        smark = int(input(f'Enter mark for student {count} : '))
        # Add the new student dict directly to the total dictionary
        totaldict[count] = {'name': sname, 'ID': sID, 'mark': smark}
    print(totaldict)

individualstudent()

What Changed?

  • Initialized totaldict: Creating an empty dictionary inside the function ensures you start fresh every time you run the function, no weird global variable conflicts.
  • Updated the loop: range(1,201) gives you 200 iterations (numbers 1 through 200) instead of just 2.
  • Simplified input prompts: Used f-strings (f'...{count}...') instead of string concatenation—they're easier to read and write.
  • Removed useless code: Got rid of that redundant studno string assignment since it wasn't doing anything.

If You Need a Global Dictionary

If you need to access totaldict outside the function (like using it in other parts of your code), you can adjust it like this:

# Initialize the global dictionary once
totaldict = {}

def individualstudent():
    global totaldict  # Tell Python we're using the global variable
    totaldict.clear()  # Clear old entries if you run the function multiple times
    for count in range(1, 201):
        sname = input(f'Enter name for student {count} : ')
        sID = int(input(f'Enter ID for student {count} : '))
        smark = int(input(f'Enter mark for student {count} : '))
        totaldict[count] = {'name': sname, 'ID': sID, 'mark': smark}
    print(totaldict)

individualstudent()

That said, using global variables can make your code harder to debug later, so the first approach is usually better for keeping things clean and self-contained.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:32:51