while nesting a dictionary inside a list, getting a TypeError : string indices must be integers
while nesting a dictionary inside a list, getting a TypeError : string indices must be integers
friend_0 = {
'first_name' : 'wong',
'last_name' : 'cap bawong',
'age' : 16,
}
friend_1 = {
'first_name' : 'lele',
'last_name' : 'cap meong',
'age' : 46,
}
friend_2 = {
'first_name' : 'kucing',
'last_name' : 'nan indah',
'age' : 18
}
people = [friend_0, friend_1, friend_2]
for person in people:
for k in person.keys():
**friend = f"{k['first_name'].title()} {k['last_name'].title()}"**
print (f"\nName: {friend}")
print (f"\tAge: {k['age']}")
我在以下行中遇到错误:friend = f"{k['first_name'].title()}
{k['last_name'].title()}"
FileType error - string indices must be integers
试试这个:
for person in people:
friend = f"{person['first_name'].title()} {person['last_name'].title()}"
print(f"\nName: {friend}")
print(f"\tAge: {person['age']}")
friend_0 = {
'first_name' : 'wong',
'last_name' : 'cap bawong',
'age' : 16,
}
friend_1 = {
'first_name' : 'lele',
'last_name' : 'cap meong',
'age' : 46,
}
friend_2 = {
'first_name' : 'kucing',
'last_name' : 'nan indah',
'age' : 18
}
people = [friend_0, friend_1, friend_2]
for person in people:
for k in person.keys():
**friend = f"{k['first_name'].title()} {k['last_name'].title()}"**
print (f"\nName: {friend}")
print (f"\tAge: {k['age']}")
我在以下行中遇到错误:friend = f"{k['first_name'].title()} {k['last_name'].title()}"
FileType error - string indices must be integers
试试这个:
for person in people:
friend = f"{person['first_name'].title()} {person['last_name'].title()}"
print(f"\nName: {friend}")
print(f"\tAge: {person['age']}")