新手关于len()问题,请求解答 5C

favorite_languages={
'jen':['python','ruby'],
'sarah':['c'],
'edward':['ruby','go'],
'phil':['python','haskell'],
}
for name,languages in favorite_languages.items():
print("\n"+name.title()+"'s favorite languages are:")
for language in languages:
print("\t"+language.title())

#if str(len('sarah'))==1:
print("Sarah's favorite language is C.")

    要求在for循环开头添加一条if语句,通过查看len(languages)的值来确定当前的被调查对象喜欢的语言是否多种,如果喜欢多种,就像以前一样显示输出;如果就一种,就相应的修改措辞,显示Sarah's favorite language is C.

    我不知道这句话如何去理解,if后面如何对一个字典中的值列表进行确认数字!求大神能给与解答~(上面带#是我自己的理解,但是执行的时候什么都不显示,编译是成功的!估计还是有问题)

对于键-值对 单独显示 我不知道是否这样的语句是就省略的~
for name,languages in favorite_languages.items():
for language in languages:
print("\t"+language.title())

for language in favorite_languages.keys():
print(language.title())

2个回答

我懂你的意思了,你是想区分is和are,对吧,可以这样改,排版我帮你做了一点修改

favorite_languages={
'jen':['python','ruby'],
'sarah':['c'],
'edward':['ruby','go'],
'phil':['python','haskell'],
}
for name,languages in favorite_languages.items():
    print("\n%40s" % (name.title()+"'s favorite languages"),end="")
    if(len(languages)==1):
        print(" is :",end=" ");
    else:
         print(" are:",end=" ");
    for language in languages:
        print(language.title(),end="\t")

图片说明

qq_37198052
qq_37198052 谢谢 但为什么执行后 看不到print打印Sarah's favorite language is C.这句话?
一年多之前 回复

谢谢 但为什么执行后 看不到print打印Sarah's favorite language is C.这句话?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐