Python Forum
Calculation Inside Dictionary Error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculation Inside Dictionary Error
#1
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 | +================+=============+==================+=================+
Reply
#2
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():.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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]
Reply
#4
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding to the dictionary inside the for-loop - weird behaviour InputOutput007 5 2,698 Jan-21-2021, 02:21 PM
Last Post: InputOutput007
  Error while checking for key in Dictionary onenessboy 5 2,666 Aug-14-2020, 01:06 PM
Last Post: onenessboy
  Is it possible to check for a key error in a python dictionary? mrsenorchuck 0 1,459 Dec-06-2019, 11:38 PM
Last Post: mrsenorchuck
  python Calculation error: 39.8-0.1=39.699999999999996 wei 2 2,079 Jun-10-2019, 10:22 AM
Last Post: wei
  How to pass a dictionary as an argument inside setup function of unittest nilaybnrj 1 3,205 May-11-2019, 03:18 AM
Last Post: keames
  Dictionary Calculation prophet11 3 2,633 Apr-22-2019, 09:15 AM
Last Post: Yoriz
  Dictionary Results Error prophet11 2 2,259 Apr-21-2019, 11:22 PM
Last Post: prophet11
  Error 'object has no attribute' when iterating thru a dictionary mrapple2020 8 6,948 Apr-08-2019, 01:36 AM
Last Post: mrapple2020
  Calculation error RustyShacklevert 4 2,854 Sep-25-2018, 04:29 AM
Last Post: RustyShacklevert
  Error while Logging on to outlook email account using Python inside VDI Shilton 0 4,299 Sep-09-2018, 06:53 AM
Last Post: Shilton

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020