风起鸿庄
2021-07-31 12:56
采纳率: 84.8%
浏览 27
已结题

请问为什么这个程序会产生这种结果?


favourite_languages={
    'jen':['python','ruby'],
    'sarah':['c'],
    'edward':['ruby','go'],
    'phil':['python','haskell'],
    }
for name,languages in favourite_languages.items():

    if(len(languages)<2):
        print(f"\n{name.title()}'s favourite language is {language.title()}")
    else:
        print(f"\n{name.title()}'s favourite languages are: ")
        for language in languages:
            print(f"\t{language.title()}")

结果如下:
Jen's favourite languages are:
Python
Ruby

Sarah's favourite language is Ruby

Edward's favourite languages are:
Ruby
Go

Phil's favourite languages are:
Python
Haskell
我不明白为什么sarah的输出不是C,而是Ruby

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • 浪客 2021-07-31 13:14
    最佳回答
    print(f"\n{name.title()}'s favourite language is {language.title()}")
    
    language少了个s,还要指定那个元素
    改为
    print(f"\n{name.title()}'s favourite language is {languages[0].title()}")
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题