简体   繁体   中英

Assignment: Python 3.3: How to call an instance from a class through user-input

My code for the expected sale value (esv) will only read the most recently entered stock, ie if I enter gm as the first stock, and ge as the second, it only reads ge, because it overwrites the data for gm. I'm not sure how to make the esv calculate for each stock entered, as it currently only calculates for the first stock entered. My idea is that it should actually occur after entering each stock, and stored in a new dictionary that contains the stock symbol as the key, and the esv as the value. However, the assignment says that this process is supposed to happen within the GetSale function....which makes it difficult. Doesn't make much sense to me to code it that way. Anyways, here is my GetSale code.

def getsale():
global names
global prices
global exposure
for key in names:
    symbol = key
    for key in exposure:
        risk_number = exposure[key][0]
        shares_held = exposure[key][1]
    for key in prices:
        purchase_price = prices[symbol][0]
        current_price = prices[symbol][1]
    esv = [-((current_price - purchase_price) - risk_number * current_price) * shares_held]
print("The estimated sale value of ", symbol, "is ", sorted(esv(),reverse = True))

EDIT: Ok, I got the answer for this from another source. I needed to create a new, empty list. Additionally, there was no need to have multiple for loops, as they all worked just fine in one. Then, I just needed to append the esv and stock symbol to my list, and sort/print it (I have it reversed so that the highest number will print). I would post the answer to my own question, but I'm required to wait a certain number of hours. So instead, here is the revised code.

def getsale():
global names
global prices
global exposure
sellGuide=[]
for key in names:
    symbol = key
    risk_number = exposure[symbol][0]
    shares_held = exposure[symbol][1]
    purchase_price = prices[symbol][0]
    current_price = prices[symbol][1]
    esv = (float(((current_price - purchase_price) - risk_number * current_price) * shares_held))
    sellGuide.append([esv, symbol])
print(sorted(sellGuide, reverse = True))

However, can anyone tell me a way to print only the very first in the list? I figured this code would work:

    print(sorted(sellGuide[0], reverse = True))

but I get the following error:

  File "D:\Python\Python Modules\stock_portfolio.py", line 43, in getsale
print(sorted(sellGuide[0], reverse = True))
TypeError: unorderable types: float() < str()

Your code should be

print(sorted(sellGuide, reverse = True)[0])

In your example, you're getting the first element in sellGuide and sorting on that. So you're running sort on an int/float, which isn't going to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM