Archive

Archive for April 9, 2019

Python Project




Python: Pluralise a Word


Loop through a dictionary and pluralise a word

One of Python Exercises on @PyBites codechalleng is to loop through a given dictionary of people and the number of games they’ve won then print out the users name and how many games they’ve won in the following format: “sara has won n games” and pluralise the word game to suit the number of games won.

we assume the dictionary is
games_won = dict={‘sara’:0, ‘bob’:1, ‘tim’:5, ‘julian’:3, ‘jim’:1}



The Code:



games_won ={‘sara’:0, ‘bob’:1, ‘tim’:5, ‘julian’:3, ‘jim’:1}
def print_game_stats(games_won):

for key,val in games_won.items():

print(“{} has won {}{p}”.format(key, val,p=” game” if val ==1 else ” games” ))

print_game_stats(games_won)









Follow me on Twitter..

Advertisements