格式化输出到列中

时间:2014-11-24 05:43:35

标签: python format multiple-columns

因此,我试图编写一个函数,要求投入三件事,投资利率和投资期限,然后将其输出到年份,利息和总数的列中。

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")

这是我到目前为止所拥有的。我只是坚持如何使用输入并将它们放入列中。

2 个答案:

答案 0 :(得分:0)

这是一个含糊不清的问题。您必须告诉我们您使用的是哪种软件,语言和工具。所以我们可以指导你。 从你提供的代码来看,它并不是明显的。“

答案 1 :(得分:0)

您只使用没有任何外部库的base 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]

也许是一个循环来继续问问题,以便你可以继续问问题,直到用户决定停止

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))

希望您觉得这个有用作为一般指南!当然你可能需要添加一些逻辑以确保你的输入将是十进制和东西,但我不能为你做你的工作hehehe(并接受我的答案=))