Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 属性错误:';列表';对象没有属性';标题';。我的代码不起作用,当我将它与答案进行比较时,它是相同的吗?_Python_List_Dictionary_Attributeerror - Fatal编程技术网

Python 属性错误:';列表';对象没有属性';标题';。我的代码不起作用,当我将它与答案进行比较时,它是相同的吗?

Python 属性错误:';列表';对象没有属性';标题';。我的代码不起作用,当我将它与答案进行比较时,它是相同的吗?,python,list,dictionary,attributeerror,Python,List,Dictionary,Attributeerror,我正在学习Eric Matthes的Python速成课程,我正在做练习6-9,名为“最喜欢的地方”。当我尝试这段代码时,我得到以下错误AttributeError:“list”对象没有属性“title” favourite_places = { 'mum': ['hong kong', 'orkney'], 'dad': ['slovenia', 'the alps', 'scotland highlands'], 'james': 'hong kong', } for name, place

我正在学习Eric Matthes的Python速成课程,我正在做练习6-9,名为“最喜欢的地方”。当我尝试这段代码时,我得到以下错误AttributeError:“list”对象没有属性“title”

favourite_places = {
'mum': ['hong kong', 'orkney'],
'dad': ['slovenia', 'the alps', 'scotland highlands'],
'james': 'hong kong',
}

for name, place in favourite_places.items():
print(name.title() + "'s favourite places are " + place.title())
但是我把它和这个练习的答案代码进行了比较,我不知道我做错了什么。这是答案中的代码

favorite_places = {
'eric': ['bear mountain', 'death valley', 'tierra del fuego'],
'erin': ['hawaii', 'iceland'],
'ever': ['mt. verstovia', 'the playground', 'south carolina']
}

for name, places in favorite_places.items():
print("\n" + name.title() + " likes the following places:")
for place in places:
    print("- " + place.title())

在答案中,他放置了.title(),它没有错误,但当我这样做时,它会。如果您能提供帮助,谢谢

您的问题在于您缺少以下内容:

for place in places:
    print("- " + place.title())
places
是一个
列表
,而
list
中的
place
是一个
str
title()
属性仅适用于
str
。所以当你这样做的时候:

for name, place in favourite_places.items():
print(name.title() + "'s favourite places are " + place.title())
您需要在
place
上进行迭代,以获取其中的
str
,从而调用
title()

像这样:

for name, place in favourite_places.items():
    print(name.title() + "'s favourite places are ")
    for p in place:
       print(p.title())
您还需要将
james
value
设置为1个元素列表,以便此项工作:

favourite_places = {
'mum': ['hong kong', 'orkney'],
'dad': ['slovenia', 'the alps', 'scotland highlands'],
'james': ['hong kong'],
}

最后,一定要小心压痕。我假设您在代码中正确地缩进了它,并且您只是将它复制到了错误的堆栈中,但是您的
print
语句需要缩进,以便它出现在循环中

您的问题在于缺少以下内容:

for place in places:
    print("- " + place.title())
places
是一个
列表
,而
list
中的
place
是一个
str
title()
属性仅适用于
str
。所以当你这样做的时候:

for name, place in favourite_places.items():
print(name.title() + "'s favourite places are " + place.title())
您需要在
place
上进行迭代,以获取其中的
str
,从而调用
title()

像这样:

for name, place in favourite_places.items():
    print(name.title() + "'s favourite places are ")
    for p in place:
       print(p.title())
您还需要将
james
value
设置为1个元素列表,以便此项工作:

favourite_places = {
'mum': ['hong kong', 'orkney'],
'dad': ['slovenia', 'the alps', 'scotland highlands'],
'james': ['hong kong'],
}
最后,一定要小心压痕。我假设您在代码中正确地缩进了它,并且您只是将它复制到了错误的堆栈中,但是您的
print
语句需要缩进,以便它出现在循环中

你可以试试

favourite_places = {
'mum': ['hong kong', 'orkney'],
'dad': ['slovenia', 'the alps', 'scotland highlands'],
'james': 'hong kong',
}

for name, place in favourite_places.items():
    print(name.title() + "'s favourite places are \n-{}\n".format( "\n-".join([i.title() for i in place]) if isinstance(place, list) else place.title() + "\n"))
问题是“妈妈”和“爸爸”是列表,而列表没有标题,因此您需要遍历其中的每一项并调用标题方法。

您可以尝试

favourite_places = {
'mum': ['hong kong', 'orkney'],
'dad': ['slovenia', 'the alps', 'scotland highlands'],
'james': 'hong kong',
}

for name, place in favourite_places.items():
    print(name.title() + "'s favourite places are \n-{}\n".format( "\n-".join([i.title() for i in place]) if isinstance(place, list) else place.title() + "\n"))
问题是“妈妈”和“爸爸”是列表,而列表没有标题,因此您需要对其中的每一项进行迭代并调用标题方法。

两个主要问题:

  • 在代码中,字典值在teo项中的类型为
    list
    ,在第三个项中的类型为
    str
    。这使得我们不可能以同样的方式对待他们
  • 在代码中,不会迭代字典值中的所有项
  • “hongkong”
    值固定为列表(带有单个元素),并在代码中包含两个嵌套循环。

    两个主要问题:

  • 在代码中,字典值在teo项中的类型为
    list
    ,在第三个项中的类型为
    str
    。这使得我们不可能以同样的方式对待他们
  • 在代码中,不会迭代字典值中的所有项

  • “hongkong”
    值固定为列表(带有单个元素),并在代码中包含两个嵌套循环。

    在代码中,您将“place”设置为列表。试着在最后一行打印“place”,你会看到它返回一个列表
    title()
    函数仅对字符串有效。在代码中,将“位置”设置为列表。试着在最后一行打印“place”,你会看到它返回一个列表
    title()
    函数只对字符串起作用。您忽略了最后一个字典值是字符串这一事实,而其他字典值是列表。看我的答案,这里有解释您忽略了最后一个字典值是字符串这一事实,其他的都是列表。看我的答案,解释列表没有标题方法,所以将“hong kong”更改为列表对他没有帮助。除了,OP将代码与一些参考设计进行比较,其中所有的值都是列表。@Amitairron我只是在我的答案中添加了一个条件,如果place不是列表。列表没有标题方法,所以正在更改“hong kong”列表对他没有帮助。除了,OP将代码与一些参考设计进行比较,其中所有值都是列表。@Amitairron我只是在我的答案中添加了一个条件,如果place不是列表。