2016-09-23 281 views
0
#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組輸出。

+0

'name'是從來沒有的'int',你需要檢查對字符串'「-999'' – AChampion

+0

@AChampion我想‘跳槽’,但沒有工作,要麼 –

+1

燦」 t複製你的錯誤...改成''-999''終止循環對我來說很好(就像''quit''一樣)。 – AChampion

回答

0

您的問題似乎是namestring,但您將其與-999進行比較,其類型爲int

如果你改變你的循環閱讀name != "-999"那麼比較的作品。您需要重構一些代碼以使其行爲符合您的要求,但這應該是一個很好的開始:)

+0

我改變了比較,以反映你的建議,但它仍然沒有把我踢出去。它仍然以公司名稱讀取「-999」。 –

+0

您可以嘗試比較name.split(),以防您獲取換行符以及輸入值? –

+0

-999不發佈輸出 - 它需要另一組輸入,然後帖子輸出 –

0

您需要在每次輸入後評估名稱的值。

stock_name = [] # make a list for stock name 
shares = [] # change also all other input variables into list type 

while True: # this will allow you to loop the input part 
    name = input() 
    if name != '-999': # this will evaluate the value of name 
     stock_name.append(name) # this will add your latest name input to the list 
     # Do the same to your other inputs 
    else: 
     break # exit your while loop 

# you need another loop here to do calculations and output 
# I think this is where your count variable should go to index your lists 
+0

我試過了,但它使股票名稱「-999」 –

+0

每次輸入到您的名稱變量它會覆蓋以前的數據。您需要將其更改爲列表類型。 – CpK

0
while name!="-999": #try this one 
    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:"))