2014-11-24 192 views
0

因此,我試圖編寫一個函數,要求3件事要投入的金額,利率和投資的持續時間,然後將其輸出到年份,利息和總計列。格式化輸出到列

def figinvest(): 
    amount = eval(input("Please give me the amount invested: ")) 
    interest = eval(input("Please give me the current interest rate as a decimal: ")) 
    duration = eval(input("Please give me the duration of the investment: ")) 
    info = [] 

輸出應該是這樣的:

Year Interest Total 

0  0.00  3000.00 

1  165.00 3165.00 

2  174.07 3339.07 

etc. 

列表項

print("Year Interest Total") 

這是我到目前爲止所。我只是停留在如何使用輸入並將其放入列中。

回答

0

這是一個含糊不清的問題。您必須告訴我們您使用的是哪種軟件,語言和工具。所以我們可以引導你。 從你提供的代碼來看,這並不明顯。

+0

它使用python – mb13 2014-11-24 06:01:21

0

嗨只使用基本python沒有任何外部庫,我會將結果存儲在列表中。

## your function to ask for user input 
def figinvest(): 
    amount = eval(input("Please give me the amount invested: ")) 
    interest = eval(input("Please give me the current interest rate as a decimal: ")) 
    duration = eval(input("Please give me the duration of the investment: ")) 
    return [amount, interest, duration] 

也許while循環不斷地問問題,這樣就可以繼續問,直到用戶的問題,決定停止

all_reply = [] 
while True: 
    ## ask question till user say N 
    singleReply = figinvest() 
    all_reply.append(singleReply) 
    anotherInvestment = input("Will you like to enter another investment- Press N to quit") 
    if anotherInvestment == "N" : 
     break 

print("Year\tInterest\tTotal") 
for reply in all_reply: 
    ## convert integer to string for join and print 
    reply = [ str(x) for x in reply] 
    print("\t".join(reply)) 

希望你發現這很有用作爲一般指導!當然,你可能需要添加一些邏輯來確保你的輸入是十進制的,但是我不能爲你做你的工作(並且接受我的答案=))