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