Feb-28-2019, 05:06 PM
To get the value from a dictionary, use dict_name[key]. In your assignment, dictShopping['soap'] will return the value '10'.
Using a variable named item allows easy switches of item name without reformatting the print string.
Using a variable named item allows easy switches of item name without reformatting the print string.
# first string formatting option dictShopping = {'soap': '10', 'bread': '5', 'shampoo': '8'} dictSelling = {'soap': '20.50', 'bread': '25.99', 'shampoo': '80'} item = 'soap' print('The shopping center has {} quantities of bread which cost {} dollars each.'.format(dictShopping[item], dictSelling[item]))In case you are using python 3.6+, this version is better - it uses f strings instead of .format()
# format string using f strings (python 3.6+) dictShopping = {'soap': '10', 'bread': '5', 'shampoo': '8'} dictSelling = {'soap': '20.50', 'bread': '25.99', 'shampoo': '80'} item = 'soap' print(f'The shopping center has {dictShopping[item]} quantities of bread which cost {dictSelling[item]} dollars each.')