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: "))
calc()
display()
name=input("\nEnter stock name OR -999 to Quit: ")
def calc():
totalpr=0
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()
main()
print("\nTotal Profit is $", format(totalpr, '10,.2f'))
最后一行代码表明了问题。 “totalpr”的显示不是例如3个输入股票的运行记录。这只是重述的利润损失。如果有人决定输入库存数据,我怎样才能准确显示运行总量?它的工作方式:有人输入'-999'的标记,然后程序将每个实例的所有profit_loss相加并打印出来。而已。
答案 0 :(得分:0)
每次调用totalpr
时,您都会重置calc
。
def calc():
totalpr=0 # resets to zero every time
# ...
totalpr=totalpr+profit_loss # same as `totalpr = 0 + profit_loss`
相反,您必须在totalpr
之外初始化calc
:
totalpr = 0 # set to zero only once
def calc():
# ...
totalpr = totalpr + profit_loss # same as `totalpr += profit_loss`