【发布时间】:2017-02-20 17:04:11
【问题描述】:
#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()”,我也希望将其转换为函数。
我的底部是正确的,但我不知道如何编辑我的代码以使其整齐地融入函数的形式。代码本身很好,我只需要找到一种方法转换为函数然后调用它。我已经把调用部分放下了,所以我在某种意义上是在倒退。
【问题讨论】:
标签: python function python-3.x while-loop