# 任何人都可以帮助我理解下面代码中的问题吗？

``````degree = {'name1': 'sam',
'cert1': 'bachelors',
'name2': 'Ann',
'cert2': 'masters',
'name3':'john',
'cert3': 'associates',
'name4': 'oz',
'cert4': 'phd'
}

print(degree)

def sch_degree(name, d_cert):
for cert in degree:
if cert == 'bachelors':
return (f"{name1} is a four year holder of a {cert1}")
else:
if cert == 'associates':
return (f"{name2} is a two year holder of a {cert2}")
else:
if cert == 'masters':
return (f"{name3} is a two year holder of a {cert3}")
else:
if cert == 'phd':
return (f"{name4} is a four year holder of a {cert4}")

sch_degree(name1, cert1)
``````
``````Traceback (most recent call last):

File "<ipython-input-238-4c32ecccc897>", line 1, in <module>
sch_degree(name1, cert1)

NameError: name 'cert1' is not defined
``````

``````degree = {'name1': 'sam',
'cert1': 'bachelors',
'name2': 'Ann',
'cert2': 'masters',
'name3': 'john',
'cert3': 'associates',
'name4': 'oz',
'cert4': 'phd'
}

cert_yr = {'bachelors': 'four',
'masters': 'two',
'associates': 'two',
'phd': 'four'
}

def sch_degree(n, c):
return f"{degree[n]} is a {cert_yr[degree[c]]} year holder of a {degree[c]}"

print(sch_degree('name1', 'cert1'))
print(sch_degree('name2', 'cert2'))
print(sch_degree('name3', 'cert3'))
print(sch_degree('name4', 'cert4'))
``````

``````sam is a four year holder of a bachelors
Ann is a two year holder of a masters
john is a two year holder of a associates
oz is a four year holder of a phd
``````

0 条评论