Python Forum

Full Version: Dictionary Results Error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear Programmers,

I hope you are having a wonderful sunday:

# the Program is meant to display the info from the dictionary, but instead is displaying letters.
Could you please help me solve the issue in my code.

mydata =  {'Stock1': [125, 772.88, 941.53],
          'Stock2': [85, 56.60, 73.04],
          'Stock3': [400, 49.58, 55.74],
          'Stock4': [235, 54.21, 65.27],
          'Stock5': [150, 124.31, 172.45]}

headers = ["Stock Symbol", "No Shares", "Purchase Price", "Current Value"]
separator1 = '+----------------+-------------+------------------+-----------------+'
separator2 = '+================+=============+==================+=================+'
print("\nStock Ownership")
print(separator1)
print('| {:14} | {:11} | {:16} | {:15} |'.format(*headers))
print(separator2)
for key, value in mydata.items():
    print('| {:14} | {:11} | {:16} | {:15} |'.format(*key, *value))
Output:
Stock Ownership +----------------+-------------+------------------+-----------------+ | Stock Symbol | No Shares | Purchase Price | Current Value | +================+=============+==================+=================+ | S | t | o | c | | S | t | o | c | | S | t | o | c | | S | t | o | c | | S | t | o | c |
Dont provide the reference. Instead just the value should suffic. Remove the *.
Read about how to use * in python

    print('| {:14} | {:11} | {:16} | {:15} |'.format(key, *value))
Thank you