python - Tying values to keys in a dictionary and then printing -
this smaller portion of main code have been writing. depending on user selection can add player informationa , print information dictionary player roster. want store information , print in format havent been able figure out how this. name **** phone number **** jersey number **** im new dictionaries have spent hours reading , searching on past couple of days dictionaries , have tried several different ways failed. have gotten closest way have setup still doesnt work right. feel storing information incorrectly dictionary starters, appreciated.
player_roster = {} def display_roster(self): #print roster if len(player_roster) != 0: x in player_roster.keys(): print('name:', x, 'phone number:', player_roster[x]) else: #print no 1 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 input toby name 444-444 phone number , 3 jersey number outputs name: toby phone number: ('444-4444', 'jersey number', 3) # output name: toby phone number: 444-4444 jersey number: 3
there things change in code keep close asked take @ this:
def display_roster(): if len(player_roster) != 0: 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 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
printing in multiple lines gives result want. stated in comments can done single print()
statement not think compact code makes difference yet.
further, len(self.player_roster) == []
line not make sense. writing true
in line. "emptiness" of team checked else:
.
finally, change way players stored in "roster" dictionary , have this: {"toby": ['444-4444', 3], ...}
Comments
Post a Comment