main()没有正常运行

时间:2016-10-13 22:21:16

标签: python function python-3.x

def load():    
    global name
    global count
    global shares
    global pp
    global sp
    global commission
    name=input("Enter stock name OR -999 to Quit: ")
    count =0
    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("\nEnter stock name OR -999 to Quit: ")

totalpr=0
def calc():
    global amount_paid
    global amount_sold
    global profit_loss
    global commission_paid_sale
    global commission_paid_purchase
    global totalpr
    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'))

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

main()

print("\nTotal Profit is $", format(totalpr, '10,.2f'))

我需要main():按顺序调用load(),calc()和display()。但是,程序在加载后停止。输出只会在没有计算或打印的情况下循环加载。

我特意指示不要在while循环块中放置calc()和display(),这可能是诱人的。另请注意,这解决了问题,但这不是我特别寻找的解决方案。

我需要做些什么才能让这个程序正常工作?

输出应该看起来像这样:

Enter stock name OR -999 to Quit: APPLE
Enter number of shares: 10000
Enter purchase price: 400
Enter selling price: 800
Enter commission: 0.04

Stock Name: APPLE
Amount paid for the stock:       $ 4,000,000.00
Commission paid on the purchase: $ 160,000.00
Amount the stock sold for:       $ 8,000,000.00
Commission paid on the sale:     $ 320,000.00
Profit (or loss if negative):    $ 3,520,000.00

Enter stock name OR -999 to Quit: FACEBOOK
Enter number of shares: 10000
Enter purchase price: 5
Enter selling price: 500
Enter commission: 0.04

Stock Name: FACEBOOK
Amount paid for the stock:       $  50,000.00
Commission paid on the purchase: $   2,000.00
Amount the stock sold for:       $ 5,000,000.00
Commission paid on the sale:     $ 200,000.00
Profit (or loss if negative):    $ 4,748,000.00

Enter stock name OR -999 to Quit: -999
Total Profit is $ 14,260,000.00

这是我得到的输出(我不想要):

====== RESTART: C:\Users\Elsa\Desktop\Homework 3, Problem 1.py ======
Enter stock name OR -999 to Quit: YAHOO!
Enter number of shares: 10000
Enter purchase price: 10
Enter selling price: 100
Enter commission: 0.04

Enter stock name OR -999 to Quit: GOOGLE
Enter number of shares: 10000
Enter purchase price: 15
Enter selling price: 150
Enter commission: 0.03

Enter stock name OR -999 to Quit: -999

Stock Name: -999
Amount paid for the stock:       $ 150,000.00
Commission paid on the purchase: $   4,500.00
Amount the stock sold for:       $ 1,500,000.00
Commission paid on the sale:     $  45,000.00
Profit (or loss if negative):    $ 1,300,500.00

Total Profit is $ 1,300,500.00
>>> 

1 个答案:

答案 0 :(得分:1)

我认为这是您可能采取的解决方案(众多之一):

  def load():    
    shares=int(input("Enter number of shares: "))
    pp=float(input("Enter purchase price: "))
    sp=float(input("Enter selling price: "))
    commission=float(input("Enter commission: "))
    return shares, pp, sp, commission


def calc(totalpr, shares, pp, sp, commission):
    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
    return (amount_paid, commission_paid_purchase, amount_sold,
            commission_paid_sale, profit_loss, totalpr)

def display(name, amount_paid, commission_paid_purchase, 
            amount_sold, commission_paid_sale, profit_loss):
    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'))

def main():
    totalpr = 0
    name=input("Enter stock name OR -999 to Quit: ")
    while name != '-999':
        shares, pp, sp, commission = load()
        am_paid, com_paid_purch, am_sold, \
        com_paid_sale, profit_loss, totalpr = calc(totalpr, shares, pp, sp, commission)
        display(name, am_paid, com_paid_purch, 
               am_sold, com_paid_sale, profit_loss)
        name=input("Enter stock name OR -999 to Quit: ")
    return totalpr
totalpr = main()

print("\nTotal Profit is $", format(totalpr, '10,.2f'))