#Fiery Elsa
#ID:899525
#Homework 2, Program 2
#Initialization
count=0
name=input("Enter stock name OR -999 to Quit:")
#Input
while name!=-999:
count=count+1
name=input("Enter stock name OR -999 to Quit:")
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)
#Output
print("Stock 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'))
程序循環,但不會在您按-999時跳出來打印輸出。我究竟做錯了什麼?無法退出while循環
理想的情況下,程序應該允許用戶輸入他/她想要的次數,直到用戶完成。例如:3組輸入產生3組輸出。
'name'是從來沒有的'int',你需要檢查對字符串'「-999'' – AChampion
@AChampion我想‘跳槽’,但沒有工作,要麼 –
燦」 t複製你的錯誤...改成''-999''終止循環對我來說很好(就像''quit''一樣)。 – AChampion