2016-07-12 102 views
0

這是我寫的主要代碼的一小部分。根據用戶的選擇,他們可以添加玩家信息,然後打印字典玩家名單中的信息。我想存儲信息,然後以這種格式打印,但我還沒有弄清楚如何做到這一點。 姓名**** 電話號碼**** 澤西島號碼**** 林新字典,但我花了幾個小時閱讀和搜索過去幾天有關字典,並嘗試了幾種不同的方式來做到這一點但失敗了。我現在已經設置了最近的方式,但仍然無法正常工作。我覺得我正在將信息錯誤地存儲到字典中,對於初學者來說,任何幫助都將不勝感激。將值綁定到字典中的鍵然後打印

player_roster = {} 
def display_roster(self):       #Print Roster 
    if len(player_roster) != 0: 
     for x in player_roster.keys(): 
      print('Name:', x, 'Phone Number:', player_roster[x]) 
    else:           #Print No One on Roster 
     len(player_roster) == [] 
     print('No names have been entered:') 

def add_player(self,):        #Enter Members Name 
    name = input('Enter New Players Name:') 
    phone_number = input('Enter Players Phone Number:') 
    jersey_number = int(input('Enter Players Jersey Number')) 
    player_roster[name] = phone_number, 'Jersey Number', jersey_number 
#If I input Toby as Name 444-444 as Phone Number and 3 as Jersey number it outputs like this 
Name: Toby Phone Number: ('444-4444', 'Jersey Number', 3) 
# I would like it to output like 
           Name: Toby 
           Phone Number: 444-4444 
           Jersey Number: 3 
+0

在你的打印功能,添加「\ n」每個鍵值對之間 –

回答

0

你幾乎在那裏。下面的修改將允許您打印,因爲你需要(和稍微更具可讀性):

class PlayerDictionary(): 
    def __init__(self): 
     pass 
    player_roster = {} 
    def display_roster(self):       #Print Roster 
     if len(self.player_roster) != 0: 
      for key, value in self.player_roster.iteritems(): 
       print(str(key) + ": " + str(value)) 
     else:           #Print No One on Roster 
      len(self.player_roster) == [] 
      print('No names have been entered:') 

    def add_player(self,): 
     self.player_roster['Name'] = input('Enter New Players Name:') 
     self.player_roster['Phone Number'] = input('Enter Players Phone Number:') 
     self.player_roster['Jersey Number'] = int(input('Enter Players Jersey Number')) 

if __name__ == "__main__": 
    player = PlayerDictionary() 
    player.add_player() 
    player.display_roster() 

稍微更易於維護的解決方案是創建一個播放器一類。設置對象上的屬性並使功能過載

class Player(object): 
    def __init__(self): 
     self.__name = "" 
     self.__phone_number = "" 
     self.__jersey_number = "" 

    @property 
    def name(self): 
     return self.__name 

    @property 
    def phone_number(self): 
     return self.__phone_number 

    @property 
    def jersey_number(self): 
     return self.__jersey_number 

    @name.setter 
    def name(self, val): 
     self.__name = val 

    @phone_number.setter 
    def phone_number(self, val): 
     self.__phone_number = val 

    @jersey_number.setter 
    def jersey_number(self, val): 
     self.__jersey_number = val 

    def __str__(self): 
     return ("Name: %s\nPhone Number: %s\nJersey Number: %s" % (str(self.__name), str(self.__phone_number), str(self.__jersey_number))) 

if __name__ == "__main__": 
    player = Player() 
    player.name = input('Enter New Players Name:') 
    player.phone_number = input('Enter Players Phone Number:') 
    player.jersey_number = int(input('Enter Players Jersey Number')) 
    print(player) 
0

我建議您更換打印語句是:

print(" Name: %s \n Phone Number: %s \n Jersey Number: %d") % player_roster[x] 
1

有一些東西,我會在你的代碼改變,但要保持這種接近你問什麼就來看看這:

def display_roster(): 
    if len(player_roster) != 0: 
     for x in player_roster.keys(): 
      print('Name:', x) 
      print('Phone Number:', player_roster[x][0]) 
      print('Jersey Number:', player_roster[x][1]) 
    else: 
     print('Roster is empty.') 
    return 


player_roster = {} 
def add_player(): 
    name = input('Enter New Players Name:\t') 
    phone_number = input('Enter Players Phone Number:\t') 
    jersey_number = int(input('Enter Players Jersey Number:\t')) 
    player_roster[name] = [phone_number, jersey_number] 
    return 

add_player() 
display_roster() 

# PRINTS: 
#Name: Toby 
#Phone Number: 444-4444 
#Jersey Number: 3 

打印在多行中給你你想要的結果。正如評論中所述,這也可以用一個print()聲明完成,但我不認爲緊湊代碼對你有什麼影響。

此外,這len(self.player_roster) == []線路沒有意義。這與簡單地將True排成一行一樣好。該團隊的「空虛」由else:進行檢查。

最後,我想稍微改變球員都存儲在「花名冊」字典的方式,有它這樣的:{"Toby": ['444-4444', 3], ...}