首页 > 解决方案 > 难以实现功能。一种可以从列表中删除项目,一种可以更新字典中的项目

问题描述

我正在尝试将功能放入我的程序中,以使我能够从字典中删除一个项目,并且可以使我能够更新字典中学生的成绩。

注释行是我需要用代码填充的部分。

以下代码是我到目前为止所拥有的:

grade_book = {
                203942: [88,78,83],
                236732: [90,78],
            9874: [78],
                29746: [79,90], 
        75893: [82,80], 
        352418: [77,88,84],
        13563: [90,89,92] 
}

while True:
    print('Select 1 to display grades')
    print('Select 2 to add new student grade')
    print('Select 3 to update a student record')
    print('Select 4 to delete a student record')
    key_pressed = input("Selection: ")
    if key_pressed == '1':
        print('{:<15}'.format(', '.join(map(str, ["Students Ids"  , "Grades"]))))
        for key, value in grade_book.items():
            print("{:<15} {:<15}".format(key, ', '.join(map(str, value))))
            pass
    elif key_pressed == '2':
        id = input('Enter new student id: ')
        grades = input('Enter the grades: ')

        grade_book[id] = [int(grade) for grade in grades.split(',')]
    #elif key_pressed == '3':


    #elif key_pressed == '4':


    elif key_pressed == '5':
        break
    else:
        print('Error!',key_pressed, 'is not a valid value')

print('Program Ended!')

这是输入“3”时的示例输出(表示您要更新学生成绩的输入):

Selection: 3

Which student do you want to update?    
1 - 203942
2 - 236732
3 - 9874
4 - 29746 
5 - 75893 
6 - 352418
7 - 13563 
Selection: 3 

Enter the grades for the student separated by commas: 93,77,62
Updated Successfully!

为此,我对如何像这样列出字典以及如何从用户那里获得输入以选择给定值之一感到困惑

接下来,这里是“4”时的示例输出(表示您要从成绩簿中删除学生的输入):

Which student do you want to delete?
1 - 203942
2 - 236732
3 - 9874
4 - 29746 
5 - 75893 
6 - 352418
7 - 13563 
Selection: 2
Deleted successfully!

同样,我对如何获取字典中的项目以及如何获取用户输入的数字感到困惑

我感谢任何和所有的帮助。如果您需要我详细说明,请询问。先感谢您!

标签: pythondictionary

解决方案


以下将功能 3 和 4 添加到您的代码中。

代码

grade_book = {
                203942: [88,78,83],
                236732: [90,78],
            9874: [78],
                29746: [79,90], 
        75893: [82,80], 
        352418: [77,88,84],
        13563: [90,89,92] 
}

while True:
    print('Select 1 to display grades')
    print('Select 2 to add new student grade')
    print('Select 3 to update a student record')
    print('Select 4 to delete a student record')
    key_pressed = input("Selection: ")
    if key_pressed == '1':
        print('{:<15}'.format(', '.join(map(str, ["Students Ids"  , "Grades"]))))
        for key, value in grade_book.items():
            print("{:<15} {:<15}".format(key, ', '.join(map(str, value))))
            pass
    elif key_pressed == '2':
        id = input('Enter new student id: ')
        grades = input('Enter the grades: ')

        grade_book[id] = [int(grade) for grade in grades.split(',')]
    elif key_pressed == '3':
      print('Which student do you want to update? ')
      print(*grade_book.keys(), sep='\n')
      student_id = input('Selection: ')
      if student_id.isdigit() and int(student_id) in grade_book:
        student_id = int(student_id)
        grades = input('Enter the grades for the student separated by commas: ')
        grades = list(map(int, grades.split(','))) # alternative to  [int(grade) for grade in grades.split(',')]
        grade_book.update({student_id: grades})
      else:
        print('Student not in grade book')

    elif key_pressed == '4':
      print('Which student do you want to delete? ')
      print(*grade_book.keys(), sep='\n')
      student_id = input('Selection: ')
      if student_id.isdigit() and int(student_id) in grade_book:
        student_id = int(student_id)
        del grade_book[student_id]
        print('Deleted successfully!')
    else:
        print('Student not in grade book')

    elif key_pressed == '5':
        break
    else:
        print('Error!',key_pressed, 'is not a valid value')

print('Program Ended!')

解释

你提到你不确定:

  1. 如何获取学生ID列表。
  2. 如何从字典中删除一个项目
  3. 如何更新字典

对于 1,获取学生 ID 列表:

print(*grade_book.keys(), sep='\n')
  • Grade_book.keys() - 是所有学生 ID 的列表
  • *grade_book.keys() - 传递打印每个 id 作为单独的参数(列表解包)
  • sep = '\n' - 在 print 中的每个参数之间放置一个

对于 2,删除字典键的几个选项,但我使用:

del grade_book[student_id]

对于 3,更新字典的几个选项,但我使用:

grade_book.update({student_id: grades})

另一种选择是:

grade_book[student_id] = grades

推荐阅读