main():
我需要 load()
按此顺序呼叫calc()
,display()
和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
放在while循环块中,这可能很诱人。另请注意,这解决了问题,但这不是我特别寻找的解决方案。
要使此程序正常运行,我需要做什么?
输出应该看起来像这样:
====== 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 :(得分:0)
第一个问题是每次执行输入语句时都会破坏变量name
。您需要将输入结果分配给临时变量,并检查是否等于-999,如下所示:
def load():
global name
global count
global shares
global pp
global sp
global commission
count =0
while True:
s=input("Enter stock name OR -999 to Quit: ")
if s == '-999':
break
name = s
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
的值将是有效的股票名称。
第二个问题是你的老师显然已经指示你不要做你需要做的事情才能使你的程序运作。如果你不能在循环中调用你需要调用的函数,它们只能运行一次。在这种情况下,逻辑上不可能为每个库存单独打印输出。我无法帮助你。
答案 1 :(得分:0)
根据您的评论
take the calc() and display() inside the loop and move them directly into def main():
这与您的预期输出一致:
def main():
name=input("Enter stock name OR -999 to Quit: ")
count, totalpr = 0, 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: "))
# calculation
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
# printing
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'))
# next loop
name=input("\nEnter stock name OR -999 to Quit: ")
print("\nTotal Profit is $", format(totalpr, '10,.2f'))
这似乎是重构的练习 - 将功能一起移动到单个功能中。