繁体   English   中英

如何为字典中的每个输入项创建一个列表 - Python?

[英]How to create a list for each inputted item in dictionary - Python?

我正在制作一个学生评分字典,其中每个学生的成绩应该存储在之前在学生 ID 下创建的字典中,以及学生的姓名。 前任。 {'1234': {'Name': 'Josh', 'Scores': [88,99,77]}} 我对编码比较陌生,所以不知道要分享多少代码,而且可能要长得多比现在需要的。

myDict = {}
total_scores=[]
all_names=[]
total_student_id=[]
for key in total_student_id: 
    for value in all_names: 
        myDict[key] = value 
        all_names.remove(value) 
        break 
for key in all_names: 
    for value in total_scores: 
        myDict[key] = value2 
        total_scores.remove(value2) 
        break 

new_student=True 
more_grades = False
while new_student:
    name = input("What is their name?")
    all_names.append(name)
    student_id = input("What is their student id?")
    total_student_id.append(student_id)
    repeat1 = input("Are there any more students? yes or no: ")
    if repeat1 == "no": 
        new_student = False
        more_grades = True
    else:
        new_student = True
while more_grades == True:
    assignments = int(input("How many assignments were given: "))
    assign_number = 0
    while assign_number < assignments: 
        score = int(input("enter one of your assignment scores: "))
        total_scores.append(score)
        assign_number = assign_number+1
        more_grades = False
        continue
for key, value in myDict.items():
    print(key, ' : ', value, value2)

目前,此代码设置为获取每个学生的输入及其 ID。 它还要求提供作业的数量以及每项作业的成绩,但它不要求每个特定学生的成绩。 我想切换此代码以单独询问每个学生的成绩,并将其添加到字典中,以获得类似于我上面提到的 output。 编辑:我也遇到了最后没有打印出任何结果的问题。 最初,它打印出学生 ID 和姓名,但是一旦我开始实施作业/分数,一旦我添加了最终分数,代码就会结束。

当前 Output:

What is their name?Andrew
What is their student id?101
Are there any more students? yes or no: yes
What is their name?Dani
What is their student id?102
Are there any more students? yes or no: yes
What is their name?George
What is their student id?103
Are there any more students? yes or no: no
How many assignments were given: 3
enter one of your assignment scores: 22
enter one of your assignment scores: 88
enter one of your assignment scores: 55

预期 Output:

Please enter the student's name: Andrew
Please enter the student's ID: 101
Enter another student? (y)es (n)o: y
Please enter the student's name: Dani
Please enter the student's ID: 102
Enter another student? (y)es (n)o: n
How many assignments were given? 3

Please enter the scores for Andrew
Enter score (0-100) for assignment 1: 90
Enter score (0-100) for assignment 2: 88
Enter score (0-100) for assignment 3: 95

Please enter the scores for Dani
Enter score (0-100) for assignment 1: 56
Enter score (0-100) for assignment 2: 70
Enter score (0-100) for assignment 3: 85

Final grade report:
Andrew's average score was 91.0
Dani's average score was 70.3

Assignment averages:
The average for assignment 1 was 73.0
The average for assignment 2 was 79.0
The average for assignment 3 was 90.0

所以我浏览了你的图片,最后我写了代码。

def main():
    students_left_to_log = int(input("How many students would you like to log?\n"))

    student_collection = {}
    while students_left_to_log > 0:
        student_id = input("Student Id: \n")
        name = input("Student Name: \n")
        assignment_left_to_log = int(input("How many assignments were given?\n"))
        assignment_counter = 1
        student_data = {'Name': name, 'Scores': []}
        while assignment_left_to_log > 0:

            # maybe use a try except block here avoid errors
            score = int(input(f"Enter their score on assignment {assignment_counter}: \n"))

            if score > 100 or score < 0:
                print("The scores must be between 0 and 100.")
                continue

            student_data['Scores'].append(score)
            # reduce the number of assignments left to log
            assignment_left_to_log -= 1
            assignment_counter += 1

        # reduce the number of students left
        students_left_to_log -= 1
        student_collection[student_id] = student_data
        print(f"Successfully logged student: {student_id}\n\n")

    for student_id, student_data in student_collection.items():
        avg_score = round(sum(student_data['Scores']) / len(student_data['Scores']), 1)
        print(f"Student {student_data['Name']} avg assignment score: {avg_score}")


if __name__ == '__main__':
    main()

您想通过以下方式了解 go 的一些概念:

  • python 中的错误处理(try except 块)
  • 字符串插值 python(漂亮的f""语法)
  • round()内置方法
  • 也许一些 JSON 基础知识(因为它与 python dict非常相似)在这里阅读

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM