不知道这是否为时已晚,但也许您可以使用字典:
majors = {}
for i in range(3):
major = input('Insert a major\n')
majors[major] = {}
print("To terminate type 'end'\n")
for major in majors:
student = ""
while student != "end":
student = input('Insert a student for major {}\n'.format(major))
if student != "end":
majors[major][student] = {}
stud_class = ""
while stud_class != "end":
stud_class = input('Insert a class for student {}\n'.format(student))
if stud_class != "end":
grade = input('Insert grade\n')
majors[major][student][stud_class] = grade
print(majors)
# Example throwing inside some random bunch of data, just so you can get the feeling
>> {'Math': {'Laura': {'IT': '10', 'English': '8'}, 'Marco': {'Spanish': '4'}}, 'English': {'Jen': {'IT': '9'}}, 'Spanish': {}}
当然,您必须在用户输入上添加验证,因为事情很容易出错。
专业的数量也是固定的,但每个学生的学生和班级不是。停止输入的一个简单解决方案可能是:当用户输入 "end" 时,您丢弃该输入并传递到下一件事。
希望这可以帮助!