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

如何编辑列表数据(简易系统):列表插入数据语法错误求助

Fixing Your Grade Management System Code

Let's break down the issues in your code first, then fix it to meet your goal of a system that can accept grades, store them in a list, and let you edit existing entries:

Key Issues in Your Original Code

  • Undefined Variable Error: The final line print(adds) references a variable adds that was never declared or assigned a value—this will throw a NameError every time.
  • Invalid Index Insertion: If your list starts empty, trying to insert at index 1 or 2 (for science/english) will cause an IndexError because the list doesn't have those positions yet.
  • Confused Edit Logic: The edit == 2 branch has an extra, unnecessary input for grade1 after inserting grade2, which makes no sense for an edit workflow. Plus, using insert adds new elements instead of replacing existing ones—you want to update the grade for a specific subject, not add a new entry.

Revised Working Code

Here's a polished version that handles adding/editing grades, avoids errors, and includes a user-friendly menu:

# Initialize list for 3 subjects: Math, Science, English (None = grade not set yet)
grades = [None, None, None]

def grade_manager():
    while True:
        print("\n=== Grade Manager ===")
        print("1. Add/Edit Math Grade")
        print("2. Add/Edit Science Grade")
        print("3. Add/Edit English Grade")
        print("4. View All Grades")
        print("5. Exit")
        
        try:
            choice = int(input("Pick an option (1-5): "))
        except ValueError:
            print("Please enter a valid number!")
            continue
        
        if choice == 1:
            math_grade = int(input("Enter Math grade: "))
            grades[0] = math_grade
            print("Math grade updated!")
        elif choice == 2:
            science_grade = int(input("Enter Science grade: "))
            grades[1] = science_grade
            print("Science grade updated!")
        elif choice == 3:
            english_grade = int(input("Enter English grade: "))
            grades[2] = english_grade
            print("English grade updated!")
        elif choice == 4:
            print("\nCurrent Grades:")
            print(f"Math: {grades[0] if grades[0] is not None else 'Not assigned'}")
            print(f"Science: {grades[1] if grades[1] is not None else 'Not assigned'}")
            print(f"English: {grades[2] if grades[2] is not None else 'Not assigned'}")
        elif choice == 5:
            print("Goodbye!")
            break
        else:
            print("Invalid option—choose 1-5.")

# Start the system
grade_manager()

What This Fixes/Improves

  • Uses direct assignment (grades[0] = math_grade) instead of insert to edit existing subject grades, which is exactly what you want for an edit function.
  • Initializes the list with None values so each subject has a fixed position from the start—no more index errors.
  • Adds a looped menu so users can perform multiple actions without restarting the script.
  • Includes error handling for non-numeric inputs (so entering text instead of a grade won't crash the program).
  • Adds a "View All Grades" option to confirm changes easily.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 21:57:42