本帖最后由 老伙计 于 2019-10-8 18:35 编辑
这条语句中的变量 language 存在未赋值就使用的问题
print('\n'+name.title()+"'s favourite language is:" + language + '.')
下面的代码我已经整体修改,楼主可以测试一下:
favourite_languages = {
'jen':['python' , 'ruby'] ,
'sarah' : ['c'] ,
'edward' : ['python' , 'go'] ,
'phil' : ['python' , 'haskell'] ,
}
for name , languages in favourite_languages . items() :
if len(languages) == 1 :
print("\n" + name . title() + "'s favourite language is : \n\t" + languages[0]) # 只有 1 个元素也是列表,同样需要通过切片操作来获取到目标字符串
else:
print("\n" + name . title() + "'s favourite language are : ")
for language in languages:
print('\t' + language . title())
|