第一个函数发生不需要的循环

时间:2016-10-12 19:36:13

标签: python function python-3.x while-loop

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()

输入佣金后,程序应立即运行计算并相应显示信息。但是现在,它只是循环而不显示/计算任何东西。

我想维护程序本身的结构 -

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

但是还有一些阻碍程序将结果载入calc然后按顺序显示的东西。

我的经验不足(这是为了上课)?加载股票信息后,它可能会在while循环中踢出。没有(在我看来)任何告诉程序进入calc()然后移动到display()的东西......我认为它是用def main():序列实现的,但我可能是错的。

这就是输出的样子:

============== 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

0 个答案:

没有答案