【问题标题】:Monthly Income and Expenses Code (Python)每月收入和费用代码(Python)
【发布时间】:2019-10-11 03:48:53
【问题描述】:

所以我是 Python 的新手,我被要求编写一个代码来根据输入的利率跟踪月收入、支出、净结果和净现值。

我试图根据 12 个月来组织它,然后为每个月的收入、费用和净值划一条线,利率将在年底公布。

到目前为止,我只能组织从 1 到 12 的月份,并且无法获得每个月内的收入或支出行,更不用说将它们连接到用户输入。任何有关后续步骤的建议将不胜感激。以下是问题的要求:

您的程序应首先询问用户月利率。然后你的程序应该在用户输入项目的一些收入和支出时循环。在用户输入完收入和支出后,例如直接按 Enter 键而不输入任何内容,您的程序应编写 (1) 用户提供任何信息的每个月的收入、支出和净结果摘要,包括月份按递增顺序; (2) 项目在第 0 个月基于汇总现金流的净现值。

假设用户输入了 1% 的月利率为“0.01”,然后用户输入了第 0 个月 200 的费用为“0 -200”,第 2 个月的收入为 1000 为“2 1000”,第 2 个月的另外收入 200 为“2 200”,第 2 个月的支出 500 为“2 -500”。在这种情况下,您的程序应该打印如下内容:

第 0 个月:
收入:0
费用:200
净结果:-200
第 2 个月:
收入:1200
费用:500
净结果:700
第 0 个月的净现值:486.21

        for i in range(1,13):
            print("Month", i,":")
    my_tab = {"Income": 0,
              "Expenses": 0}
    incomes_list = []
    expenses_list = []

    interest_rate = input("Please enter the interest rate")
    while True:
        input_month_and_value = input("Please enter a month from 1-12 and the corresponding income or expense")
        if input_month_and_value == "":
            break
        input_month_and_value_list = input_month_and_value.split()
        month = (input_month_and_value_list[0])
        value = float(input_month_and_value_list[1])
        if month in my_tab:
            my_tab[month] += value
        else:
            my_tab[month] = value
        if value > 0:
            incomes_list.append(input_month_and_value)
        if value < 0:
            expenses_list.append(input_month_and_value)

        print(my_tab)

【问题讨论】:

  • 嗨,Jay,欢迎来到 Stack Overflow。非常感谢您提出的详细问题,但是,阅读这篇文章非常重要。如果您可以专注于(1)每个帖子的特定问题,并且只包含最相关的细节,那将是很好的,这样人们就可以最好地帮助您。以下链接可帮助指导您构建帖子:link

标签: python


【解决方案1】:

这是一些执行基本功能的示例代码

import sys
from collections import defaultdict
import numpy as np 

def get_monthlys():
  " Places inputs as list of tuples (month, amount) "
  inputs = []

  while True:
    input_val = input("Month (1-12) and income or expense (space separated): ")
    if input_val == "":
      break
    month, amount = input_val.split()
    month, amount = int(month), float(amount)
    inputs.append((month, amount))

  return inputs

def groupby_month(inputs):
  " Groups cash flow for each month "
  cashflow = defaultdict(list)
  for month, amount in inputs:
    cashflow[month].append(amount)

  return cashflow

def show_monthlys(cashflow):
  s = sorted(cashflow.items(), key = lambda x: x[0])
  for month, amounts in s:
    incomes = [v for v in amounts if v >= 0]
    expenses = [-v for v in amounts if v < 0]
    net = sum(incomes) - sum(expenses)
    print(f'Month: {month}')
    print(f'Incomes: {" ".join(map(str, incomes)) if incomes else None}')
    print(f'Expenses: {" ".join(map(str, expenses)) if expenses else None}')
    print(f'Net for Month: {net}')
    print()

def calculate_npv(interest_rate, cashflow):
  " NPV "
  min_month, max_month = min(cashflow.keys()), max(cashflow.keys())

  totals = np.zeros(max_month - min_month + 1, dtype = 'f')

  for month, amounts in cashflow.items():
    month_index = month - min_month
    totals[month_index] = sum(amounts)

  return np.npv(interest_rate, totals)

# Get inputs
rate_input = input("Monthly Interest rate: ")
try:
   interest_rate = float(rate_input)
except ValueError:
   print("Error-->> interest rate should be numeric")
   sys.exit()

inputs = get_monthlys()
cashflow = groupby_month(inputs)

# Monthly Reports
show_monthlys(cashflow)

# Net Present Value
print(f'Net Present Value: {calculate_npv(interest_rate, cashflow):.2f}')

示例运行(月份可以任意顺序输入,甚至可以跳过月份)

输入:

Monthly Interest rate: .01
Month (1-12) and income or expense (space separated): 1 200.5
Month (1-12) and income or expense (space separated): 2 300
Month (1-12) and income or expense (space separated): 3 125.4
Month (1-12) and income or expense (space separated): 1 -95
Month (1-12) and income or expense (space separated): 4 650.25
Month (1-12) and income or expense (space separated): 5 95.77
Month (1-12) and income or expense (space separated): 6 800.25
Month (1-12) and income or expense (space separated): 1 155.65
Month (1-12) and income or expense (space separated): 2 -99.99
Month (1-12) and income or expense (space separated): 5 295.77
Month (1-12) and income or expense (space separated): 8 901
Month (1-12) and income or expense (space separated): 9 1024
Month (1-12) and income or expense (space separated): 8 -200
Month (1-12) and income or expense (space separated):

输出:

Month: 1
Incomes: 200.5 155.65
Expenses: 95.0
Net for Month: 261.15

Month: 2
Incomes: 300.0
Expenses: 99.99
Net for Month: 200.01

Month: 3
Incomes: 125.4
Expenses: None
Net for Month: 125.40

Month: 4
Incomes: 650.25
Expenses: None
Net for Month: 650.25

Month: 5
Incomes: 95.77 295.77
Expenses: None
Net for Month: 391.54

Month: 6
Incomes: 800.25
Expenses: None
Net for Month: 800.25

Month: 8
Incomes: 901.0
Expenses: 200.0
Net for Month: 701.00

Month: 9
Incomes: 1024.0
Expenses: None
Net for Month: 1024.00

Net Present Value: 3950.39

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多