-1
self.biography = {'name' : '', 'age' : 0, 'nationality' : '', 'position' : '', 'footed' : ''}
def create_player(self):
name = input('Player name: ')
age = int(input('Player age: '))
nationality = input('Nationality: ')
position = input('Position: ')
footed = input('Footed: ')
bio_attributes = [name, age, nationality, position, footed]
for attribute in bio_attributes:
self.biography[attribute] = bio_attributes
我有点想通过这条巨蟒方法进行迭代,然后将值到词典中,我知道我可以做类似遍历用户输入和插入到Python字典
self.biography["name"] = name
self.biography["age"] = age
但这显然重复的代码,所以我如何使用像我的循环,使这项工作?
我已经回答了http://stackoverflow.com/questions/37069871/append-to-python-dictionary-from-method/37069996#37069996 –