favorite_places = {
'xiaowang' : ['a1'],
'xiaoli' : ['a1','b2','c3'],
'xiaozhang' : ['c3','d4']
}
for names ,places in favorite_places.items() :print(len(places))
##为什么xiaowang的len(places)是2?????
##是因为a1有两个字符吗?那么怎么把a1当作一个字符呢?
##通过给a1加上[],使得a1变成列表可以解决这个问题,还有别的办法吗?
if len(places) > 1 :
print(names.title() + "'s favorite places are : \n")
for place in places :
print('-' + place)
elif len(places) == 1 :
print(names.title() + "'s favorite places is : \n")
print('-' , places)
##如果是('-'+places)就会出现:
##TypeError: can only concatenate str (not "list") to str
##改成('-' , places)错误解决,还有别的解决办法吗?
##怎么能去掉输出结果['a1']的方括号呢?
问题都标记在注释中了,初学编程希望能得到各位的帮助,谢谢大家