0
def load():    
    name=0
    count=0
    totalpr=0
    name=input("Enter stock name OR -999 to Quit: ")
    while name != '-999':
        count=count+1
        shares=int(input("Enter number of shares: "))
        pp=float(input("Enter purchase price: "))
        sp=float(input("Enter selling price: "))
        commission=float(input("Enter commission: "))
        name=input("Enter stock name OR -999 to Quit: ")

def calc():
    totalpr=0
    amount_paid=shares*pp
    commission_paid_purchase=amount_paid*commission
    amount_sold=shares*sp
    commission_paid_sale=amount_sold*commission
    profit_loss=(amount_sold - commission_paid_sale) -(amount_paid + commission_paid_purchase)
    totalpr=totalpr+profit_loss

def display():
    print("\nStock Name:", name)
    print("Amount paid for the stock:       $",      format(amount_paid, '10,.2f'))
    print("Commission paid on the purchase: $", format(commission_paid_purchase, '10,.2f'))
    print("Amount the stock sold for:       $", format(amount_sold, '10,.2f'))
    print("Commission paid on the sale:     $", format(commission_paid_sale, '10,.2f'))
    print("Profit (or loss if negative):    $", format(profit_loss, '10,.2f'))
    print("Total Profit is $", format(totalpr, '10,.2f'))

def main():
    load()
    calc()
    display()

main()

Immediately after commission is entered, the program should run the calculations and display the information accordingly. But right now, it's simply looping and not displaying/calculating anything.

I would like to maintain the structure of program itself --

def main():
load()
calc()
display()

But there is something else impeding the program from carrying the results of load into calc and then into display in that order.

My inexperienced (this is for a class) thought? It might be something with kicking out of the while loop after the stock information is loaded. There isn't (in my opinion) anything telling the program to move onto calc() and then move onto display()...I thought it was being achieved with the def main(): sequence but I could be wrong.

This is what the output should look like:

============== RESTART: C:\Users\ELSSAAAAA\Desktop\Sample.py ==============
Enter stock name OR -999 to Quit: GOOGLE
Enter number of shares: 10000
Enter purchase price: 30
Enter selling price: 300
Enter commission: 0.04

Stock Name: GOOGLE
Amount paid for the stock:       $ 300,000.00
Commission paid on the purchase: $  12,000.00
Amount the stock sold for:       $ 3,000,000.00
Commission paid on the sale:     $ 120,000.00
Profit (or loss if negative):    $ 2,568,000.00

Enter stock name OR -999 to Quit: AMAZON
Enter number of shares: 10000
Enter purchase price: 25
Enter selling price: 250
Enter commission: 0.04

Stock Name: AMAZON
Amount paid for the stock:       $ 250,000.00
Commission paid on the purchase: $  10,000.00
Amount the stock sold for:       $ 2,500,000.00
Commission paid on the sale:     $ 100,000.00
Profit (or loss if negative):    $ 2,140,000.00

Enter stock name OR -999 to Quit: -999
Total Profit is $ 14,260,000.00
  • So when are you entering "-999"? – UnholySheep Oct 12 '16 at 19:38
  • Edited to reflect that. –  Oct 12 '16 at 19:39
  • 1
    Or rather (if I understand you correctly) you want to have the `while` loop **around** the 3 functions in `main`, not inside of `load` – UnholySheep Oct 12 '16 at 19:39
  • So def load(): , def calc(): and def display(): would occur *inside* the while loop? –  Oct 12 '16 at 19:43
  • That is what you are trying to achieve, is it not? (at least based on the desired output you have posted) – UnholySheep Oct 12 '16 at 19:44
  • Ok, so I tried that but I only get the stock name requested then the program goes blank. –  Oct 12 '16 at 19:46
  • Actually you should be receiving tons of errors, as you are trying to access variables that are not defined in your functions (e.g.: `calc` is trying to access `pp` which is local to `load`). This is the point where I would redirect you to a basic Python tutorial, as it far exceeds the scope of the question. – UnholySheep Oct 12 '16 at 20:18
  • Should I convert all the variables into Global variables? –  Oct 12 '16 at 20:54

0 Answers0