将while循环的内容转换为函数

时间:2016-10-12 02:00:20

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

#Initialization
name=0
count=0
totalpr=0.0
#Load
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: "))
    #Calculations
    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
    #Output
    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'))
    name=input("\nEnter stock name OR -999 to Quit: ")
print("Total Profit is $", format(totalpr, '10,.2f'))

def main()

load() #to input the values
calc()
print()

正如您所看到的,我不知道如何将段落“#Calculations”的内容转换为函数,以便可以在底部调用它。 “#Load”也是如此(我想以某种方式在那里输入所有输入,即使它之间有一个while循环)。而对于“#Output”,也称为“print()”,我希望它也可以转换为函数。

我已将底部部分向右下方,但我不知道如何编辑我的代码以使其完全符合函数的形式。代码本身很好,我只需要找到一种转换为函数然后调用它的方法。我把呼叫部分打倒了,所以我从某种意义上说是倒退了。

2 个答案:

答案 0 :(得分:0)

这是将while循环内部转换为函数的一种方法,然后使用main函数根据需要多次调用该函数。

您可以将此代码进一步分解为打印功能。只需使用return语句即可。

def calculate(shares,pp,sp,commission,name):
    totalpr = 0
    amount_paid=int(shares)*float(pp)
    commission_paid_purchase=float(amount_paid*commission)
    amount_sold=int(shares)*float(sp)
    commission_paid_sale=float(amount_sold*commission)
    profit_loss=float(amount_sold - commission_paid_sale) -float(amount_paid + commission_paid_purchase)
    totalpr=totalpr+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'))
    print("Total Profit is $", format(totalpr, '10,.2f'))
def main():
    name = input("Enter stock:")
    while name != '-999':
        calculate(5,1.22,11.33,11.44,name)
        name = input("Enter stock:")
main()

答案 1 :(得分:0)

对于你的几行代码没有意义,因为它不必要地炸毁了一些东西,但是你去了:

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):
    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+=profit_loss
    return commission_paid_purchase, amount_paid, amount_sold, commission_paid_sale, profit_loss, totalpr

def output():
    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'))


if __name__ == "__main__":
    #Initialization
    name=0
    count=0
    totalpr=0.0
    name=input("Enter stock name OR -999 to Quit: ")
    while name!='-999':
        count+=1
        #Load
        shares, pp, sp, commission = load()
        #Calculations
        commission_paid_purchase, amount_paid, amount_sold, commission_paid_sale, profit_loss, totalpr = calc(totalpr)
        #Output
        output()
        name=input("\nEnter stock name OR -999 to Quit: ")
print("Total Profit is $", format(totalpr, '10,.2f'))

请注意,在while循环范围内定义的变量会自动从该循环中调用的函数可见。