[英]Check if a student has passed a class
I have a list/dictionary within a dictionary, and I want to check if a specific person has passed the subject or not.我在字典中有一个列表/字典,我想检查一个特定的人是否通过了该主题。 The dictionary looks like this:
字典看起来像这样:
students = {
'Peter': ['Economy', {'PRO100': 'B', 'PRO110': 'C', 'DAT130': F}],
'James': ['Psychology', {'MAT120': C, 'PRO100': B, 'DAT120': A}]
}
A is the best grade, and F is failed. A是最好成绩,F是不及格。 'Economy' and 'Psychology' shows which department the subjects belongs to.
“经济”和“心理学”显示科目所属的部门。
I want a function like this:我想要这样的 function:
def check(student, subject)
where I can check passed/failed as this:我可以在这里检查通过/失败:
check('Peter', 'PRO100')
>>> True
check('Peter', 'DAT130')
>>> False
I think I can use a for-loop within the function, but I don't know how...我想我可以在 function 中使用 for 循环,但我不知道如何......
Here is the answer to your code:这是您的代码的答案:
students = {
'Peter': [
'Economy', {
'PRO100': 'B',
'PRO110': 'C',
'DAT130': 'F'}],
'James':
['Psychology', {
'MAT120': 'C',
'PRO100': 'B',
'DAT120': 'A'}]
}
good_grades = ['A', 'B', 'C']
bad_grades = ['D', 'E', 'F']
def check(student, subject):
for subjects in students[student]:
subjectList = subjects
for grade in subjectList:
if grade == subject:
if subjectList[grade] in good_grades:
return True
if subjectList[grade] in bad_grades:
return False
return 'Not Found'
check('Peter', 'PRO100')
Examples:例子:
check('Peter', 'DAT130')
False
check('James', 'DAT120')
True
Try this code:试试这个代码:
students = {
'Peter': ['Economy', {'PRO100': 'B', 'PRO110': 'C', 'DAT130': 'F'}],
'James': ['Psychology', {'MAT120': 'C', 'PRO100': 'B', 'DAT120': 'A'}]
}
passed_grades = ['A', 'B', 'C']
def check(student, subject):
if students[student][1][subject] in passed_grades:
return True
return False
I wrote this code:我写了这段代码:
def check(student, subject):
x = students.get(student)
y = x[1]
failed = 'F'
if subject in y:
if failed in y[subject]:
print('Failed')
else:
print('Passed')
check('Peter', "DAT130")
What do your think about it?你怎么看? What could be improved?
有什么可以改进的? All feedback is appreciated:)
感谢所有反馈:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.