Python Forum
Calculation Inside Dictionary Error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Calculation Inside Dictionary Error (/thread-17732.html)



Calculation Inside Dictionary Error - prophet11 - Apr-22-2019

Dear Collegues,

Thank you in advance for your help. My code is not generating the profitability of this calculation:

Current Value - Purchase Price X No of Shares for my Dictionary.

Any support is highly appreciated!

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"]
headers1 = ["Stock Symbol", "No Shares", "Earnings/Loss"]
separator1 = '+----------------+-------------+------------------+-----------------+'
separator2 = '+================+=============+==================+=================+'


print("\nStock Earnings/Losses ")
print(separator1)
print('| {:14} | {:11} | {:15} |'.format(*headers1))
print(separator2)
for element in mydata:
    s = (element[3] - element[2]) * element[1]
    print('| {:14} | {:11} | ${:14,.2f} |'.format(element[0], element[1], s))
print(separator2)
Output:
Traceback (most recent call last): File "<input>", line 18, in <module> TypeError: unsupported operand type(s) for -: 'str' and 'str' Stock Earnings/Losses +----------------+-------------+------------------+-----------------+ | Stock Symbol | No Shares | Earnings/Loss | +================+=============+==================+=================+



RE: Calculation Inside Dictionary Error - ichabod801 - Apr-22-2019

Iterating through a dictionary iterates through it's keys. To iterate through it's values, use for value in mydict.values():. To iterate through both, use for key, value in mydict.items():.


RE: Calculation Inside Dictionary Error - prophet11 - Apr-22-2019

Dear Ichabod801,
Thank you for replying. Honestly, I'm a bit tense with this calculation. I just cannot figure out how to fix it.
Any extra mile support is highly appreciated.




[/output]


RE: Calculation Inside Dictionary Error - perfringo - Apr-22-2019

Just test what ichabod801 wrote:

In [1]: 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]}                               

In [2]: for element in mydata: 
   ...:     print(element) 
   ...:                                                                          
Stock1
Stock2
Stock3
Stock4
Stock5

In [3]: for element in mydata: 
   ...:     print(element[3], element[2], element[1]) 
   ...:                                                                          
c o t
c o t
c o t
c o t
c o t
So you accessing indexes on keys, not values. Follow ichabod801 advice and you are good to go.