格式化输出到列

问题描述:

因此,我试图编写一个函数,要求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

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

嗨只使用基本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)) 

希望你发现这很有用作为一般指导!当然,你可能需要添加一些逻辑来确保你的输入是十进制的,但是我不能为你做你的工作(并且接受我的答案=))