2016-05-06 37 views
-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のメソッドを反復処理し、その後、辞書に値を配置したい、私はユーザー入力を反復処理し、Pythonの辞書に

self.biography["name"] = name 
self.biography["age"] = age 

ような何かを行うことができます知っているが、これは明らかにコードを繰り返すので、どのように私のforループのようなものを使ってこの作業をすることができますか?

+3

私は既に回答済みです。http://stackoverflow.com/questions/37069871/append-to-python-dictionary-from-method/37069996#37069996 –

答えて

0

このような意味ですか?

self.biography = {'Player name' : '', 'Player age' : 0, 'Nationality' : '', 'Position' : '', 'Footed' : ''} 
def create_player(self): 
    for key in self.biography: 
     val = input(key + ': ') 
     self.biography[key] = val