所以我有一个类似的问题,在另一个线程中得到了回答。
如何在 Python 中让用户选择要更新的键然后选择新值来更新字典值?
基本上,如何通过 raw_input 更改嵌套字典值。我使用了该解决方案并且效果很好,但我想使用类编写程序。因此,我使用基本相同的代码创建了一个带有用于编辑字典的方法的类,但是当我尝试在类方法中运行它时,它现在给了我一个“关键错误”。
因此,在主要功能中,上述链接问题中的解决方案效果很好。但是在类方法中:
class team: # create a class where each team will be an instance
def __init__(self, name):
self.name = name #name of team will be passed from main
self.list_of_players = [] # create a list of the players
self.position1 = {} # create a dictionary for each of the positions on that team
self.position2 = {}
self.roster = [self.position1, self.position2]
def addplayer(self, player_name): # the name of the player is passed to this method from main
print 'add stats' # fill out the appropriate stats through raw_input
stat1 = raw_input('stat1: ')
stat2 = raw_input('stat2: ')
pos = raw_input('POS: ')
vars()[player_name] = {'stat1' : stat1, 'stat2' : stat2, 'POS' : pos} #create a dictionary
# for the player where all his stats are kept
player = {player_name : vars()[player_name]} # create a dictionary that will show the
# player's name as a string and his stats which are held in the dictionary named after him
self.list_of_players.append(player) # append the new player to the list of players
if pos == 'p1': # add the player and his stats to the appropriate position on the team
self.position1[player_name] = player
elif pos == 'p2':
self.position2[player_name] = player
else:
pass
def editplayer(self, player_name): # player's name is passed to the edit function from main
print self.list_of_players # player's name shows up in the list of players for the team
edit_stat = raw_input('which stat? ') # choose which stat(key) to edit via raw input
new_value = raw_input('new value: ') # choose the new value to apply to the chosen key
vars()[player_name][edit_stat] = new_value # here is where it gives a key error! this worked
#in fact even trying to call and print the players name gives the key error.
#player = vars()[player_name]
#print player
def main(): # the main function
loop1 = 0 # creating a loop so one can come back and edit the teams after creating them
list_of_teams = [] # initializing list of teams
while loop1 < 1:
print list_of_teams # show the user what teams are available to choose from
team_option = raw_input('new team or old: ') # create a new team or work with an old one
if team_option == 'new':
team_name = raw_input('team name? ') # get the team name from raw_input
vars()[team_name] = team(team_name) #create an instance of this team name
list_of_teams.append(team_name) # add the team to the list
else:
team_name = raw_input('which team? ') # choose which existing team to work with
player_choice = raw_input('new player or old? ') # choose to create or edit existing player
player_name = raw_input('player_name? ') # choose which player from raw_input
if player_choice == 'new':
vars()[team_name].addplayer(player_name) # give player_name to addplayer method
print vars()[team_name].list_of_players # shows the new player in the appropriate
# instance's roster. This method seems to be working fine
else:
vars()[team_name].editplayer(player_name) # gives the player's name to the editplayer
# method for the appropriate instance. But the player name just raises a key error in
# edit player method. I am baffled.
print vars()[team_name].list_of_players
if __name__ == '__main__':
main()
当它只是一个长功能时,它起作用了,但看起来像一场灾难。试图学习更好的 OOP 实践,但我不知道如何通过玩家的名字调用该字典来更改值。在过去的几天里,我一直在复习关于类和字典的教程和问题,但显然我误解了一些关于变量如何从函数传递到方法的问题。
事实上,它甚至不会将字典 vars()[player_name] 分配给要打印的 var,这意味着它没有将其识别为我认为在 addplayer 方法中创建的字典。但它仍然在玩家列表中列出该字典的事实意味着它存在于该实例中。那么,当我尝试在 editplayer 方法中解决它时,为什么它不能识别它呢?以及如何调用在一种方法中创建的嵌入字典,以在第二种方法中更改该字典中的值?
Karl 指出了需要澄清的优点:这就是我想要的属性。
self.name-我想要为每个创建的团队创建一个实例
self.list of player - 每支球队都应该有自己的球员名单,这些球员名单是保存该人统计数据的字典。所以 team1 应该有自己的列表。team2 不同的列表等
self.position1/2 - 每支球队的球员将被归档在他们的不同位置字典中。所以球员 joe montana 的统计字典可以在该球队的四分卫字典中找到
self.roster - 应该是该团队按位置分组的名册。所以调用 print team1.roster 应该打印那些按位置分组的球员