我在为这个问题的第二部分构建逻辑时遇到了麻烦 [英] I am having trouble in building the logic for the 2nd part of this question

查看:131
本文介绍了我在为这个问题的第二部分构建逻辑时遇到了麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

梦想之家

编写一个程序,计算您需要多长时间才能存入足够的钱来支付梦想房屋的首付款。你会问用户他们的年薪,他们想要储蓄的工资百分比,以及他们梦想家园的成本。



你应该假设首付款是房屋总费用的25%。时间应以月为单位表示。提示:年薪/ 12将给你月薪。



第1部分 - 加薪

使用前一部分的代码作为起点。



从最后一个问题扩展计算器,让我们假设(准确地说)你大约两年(每六个月)加薪一次。记住这一点,提示用户半年度加薪百分比(每六个月会申请一次)例如,如果你从10万开始并且每六个月加薪5%,你前六个月的工资将是未来六个月8,333.33 /月(基本100,000美元)和8,750 /月(基本105,000美元),等等...



样本输出1:



你的年薪是多少? 120000

你能节省多少薪水? 10%

您的梦想住宅需要多少钱? 1000000

半年度加薪百分比是多少? 0

投资年回报是多少? 4



您需要工作183个月才能为您梦想中的房子支付首期款项。

183个月或15年零3个月。

样本输出2:



你的年薪是多少? 150000

你能节省多少薪水? 10

您的梦想住宅需要多少钱? 500000

半年度加薪百分比是多少? 3%

投资年回报是多少? 4%



您需要工作75个月才能支付您梦想中的房子的首期付款。

75个月或6年3个月。



样本输出3:



你的年薪是多少? 120000

你能节省多少薪水? 10%

您的梦想住宅需要多少钱? 1000000

半年度加薪百分比是多少? 0

投资年回报是多少? 0%



您必须工作250个月才能支付您梦想中的房子的首期付款。

250个月或20年10个月。



第2部分 - 节省一些费用



您可以重复使用前一部分的一些代码。



之前的问题集中在您需要多长时间才能支付首期付款。在这个问题中,你将回答相反的问题 - 我需要多少钱才能在X年内支付首期款项。



我们可以熨烫之前提出一些细节,以便更容易:半年度加薪将为7%投资的年回报率为4%房屋的首期付款将为25%梦想之家的成本为100万美元。



对于这个问题,您应该找到最优惠的储蓄率,以便在3年(36个月)内支付100万美元的首期付款,考虑到用户的工资。



样品运行1:



你的年薪是多少? 120000

你能节省多少薪水? 10%

您的梦想住宅需要多少钱? 1000000

半年度加薪百分比是多少? 0

投资年回报是多少? 4



您需要工作183个月才能为您梦想中的房子支付首期款项。

183个月或15年零3个月。



你打算多少年来为首期付款储蓄? 3

最优惠的储蓄率为0.6548。



样品运行2:



你的年薪是多少? 150000

你能节省多少薪水? 120

你能节省多少薪水? 12%

您的梦想住宅需要多少钱? 500000

半年度加薪百分比是多少? -3%

半年度加薪百分比是多少? 3%

投资年回报是多少? 7%



您必须工作62个月才能支付您梦想中的房子的首期付款。

62个月或5年2个月。



你打算多少年来为首期付款储蓄? 4

最优惠的储蓄率是0.1639。



样品运行3:



你的年薪是多少? 150000

你能节省多少薪水? 10%

您的梦想住宅需要多少钱? 1000000

半年度加薪百分比是多少? 7%

投资年度回报是多少? 4



您必须工作99个月才能支付您梦想中的房子的首期付款。

99个月或8年零3个月。



你打算多少年来为首期付款储蓄? 3

最优惠的储蓄率是0.4411。



我的尝试:



这是我用于第1部分的代码 -



 salary = int(输入(你的年薪是多少?))
sav_percent = int(输入(你将节省多少工资?))

monthly_sal = salary / 12#计算每月工资
储蓄= 0#初始储蓄

成本= int(输入(你的梦想家庭花多少钱?))
dwn_pymt =(0.25)* cost#to计算梦想家庭的预付款

months_of_saving = 0
raise_percent = int(输入(什么是半年度加薪百分比?))

annual_ret =(int(输入(什么是投资年度回报?))/ 100)

,同时保存< dwn_pymt:
储蓄+ =(保存*(annual_ret / 12))#这会将投资的利益加到储蓄中
储蓄+ =((sav_percent / 100)* monthly_sal)#这将添加如果months_of_saving%6 == 0:#检查半月增量
salary = salary +(salary *(raise_percent / 100))
monthly_sal = salary / 12
#print(round(salary))
#print(总数。月{},总计将保存的金额{}。。format(months_of_saving,round(saving)))

print(\ n)
print(你必须为{几个月可以为你梦想中的房子支付首期款项。。format(months_of_saving))

print({}月或{}年和{}个月.format(months_of_saving,(months_of_saving //) 12),(周一ths_of_saving%12)))

解决方案

100,000),和8,750 /月(base


105,000)接下来的六个月,等等...



样本输出1:



你的年薪是多少? 120000

你能节省多少薪水? 10%

您的梦想住宅需要多少钱? 1000000

半年度加薪百分比是多少? 0

投资年回报是多少? 4



您需要工作183个月才能为您梦想中的房子支付首期款项。

183个月或15年零3个月。

样本输出2:



你的年薪是多少? 150000

你能节省多少薪水? 10

您的梦想住宅需要多少钱? 500000

半年度加薪百分比是多少? 3%

投资年回报是多少? 4%



您需要工作75个月才能支付您梦想中的房子的首期付款。

75个月或6年3个月。



样本输出3:



你的年薪是多少? 120000

你能节省多少薪水? 10%

您的梦想住宅需要多少钱? 1000000

半年度加薪百分比是多少? 0

投资年回报是多少? 0%



您必须工作250个月才能支付您梦想中的房子的首期付款。

250个月或20年10个月。



第2部分 - 节省一些费用



您可以重复使用前一部分的一些代码。



之前的问题集中在您需要多长时间才能支付首期付款。在这个问题中,你将回答相反的问题 - 我需要多少钱才能在X年内支付首期款项。



我们可以熨烫之前提出一些细节,以便更容易:半年度加薪将为7%投资的年回报率为4%房屋的首期付款将为25%梦想房屋成本


< blockquote> 1m。



对于这个问题,你应该找到最好的节省率,以便


Dream house
Write a program that calculates how long it would take you to save enough money to pay the down payment for your dream house. You will ask the user for their annual salary, the percent of the salary they’d want to save, and the cost of their dream home.

You should assume that the down payment is 25% of the total cost of the home. The time should be expressed in months. Hint: annual salary / 12 will give you the monthly salary.

Part 1 - Get a raise
Use your code from the previous part as a starting point.

Extending the calculator from the last question, lets assume (accurately so) that you get a raise about twice year (every six months). Keeping this in mind, prompt the user for a semi-annual raise percentage (which will apply every six months) For example, if you start at 100,000 and get a 5% raise every six months, your salary for the first six months would be 8,333.33/month(base $100,000), and 8,750/month (base $105,000) for the following six months, and so on…

Sample output 1:

What's your annual salary? 120000
What percent of your salary will you save? 10%
How much does your dream home cost? 1000000
What's the semi-annual raise percentage? 0
What's the investment annual return? 4

You would have to work for 183 months to afford the downpayment for your dream house.
183 months or 15 years and 3 months.
Sample output 2:

What's your annual salary? 150000
What percent of your salary will you save? 10
How much does your dream home cost? 500000
What's the semi-annual raise percentage? 3%
What's the investment annual return? 4%

You would have to work for 75 months to afford the downpayment for your dream house.
75 months or 6 years and 3 months.

Sample output 3:

What's your annual salary? 120000
What percent of your salary will you save? 10%
How much does your dream home cost? 1000000
What's the semi-annual raise percentage? 0
What's the investment annual return? 0%

You would have to work for 250 months to afford the downpayment for your dream house.
250 months or 20 years and 10 months.

Part 2 - Build some savings

You can reuse some of your code from the previous part.

The previous questions focused on how long you would have to work to afford the downpayment. In this question, you’re going to answer the reverse - how much would I have to save to be able to afford the downpayment in X years.

We can iron out some details prior, to make this easier: The semi-annual raise will be 7% The investments have an annual return of 4% The downpayment for the house will be 25% The dream house costs $1m.

For this question, you should find the best rate of savings so that the $1m downpayment can be paid in 3 years (36 months), given the user’s salary.

Sample run 1:

What's your annual salary? 120000
What percent of your salary will you save? 10%
How much does your dream home cost? 1000000
What's the semi-annual raise percentage? 0
What's the investment annual return? 4

You would have to work for 183 months to afford the downpayment for your dream house.
183 months or 15 years and 3 months.

How many years are you planning to save for downpayment? 3
The best savings rate for you is 0.6548.

Sample run 2:

What's your annual salary? 150000
What percent of your salary will you save? 120
What percent of your salary will you save? 12%
How much does your dream home cost? 500000
What's the semi-annual raise percentage? -3%
What's the semi-annual raise percentage? 3%
What's the investment annual return? 7%

You would have to work for 62 months to afford the downpayment for your dream house.
62 months or 5 years and 2 months.

How many years are you planning to save for downpayment? 4
The best savings rate for you is 0.1639.

Sample run 3:

What's your annual salary? 150000
What percent of your salary will you save? 10%
How much does your dream home cost? 1000000
What's the semi-annual raise percentage? 7%
What's the investment annual return? 4

You would have to work for 99 months to afford the downpayment for your dream house.
99 months or 8 years and 3 months.

How many years are you planning to save for downpayment? 3
The best savings rate for you is 0.4411.

What I have tried:

Here is the code that I used for part 1 -

salary = int(input("What is you annual salary? "))
sav_percent = int(input("What percent of your salary will you save? "))

monthly_sal = salary/12                                     # calculating monthly salary
saving = 0                                                  # initial savings

cost = int(input("How much does your dream home cost? "))
dwn_pymt = (0.25)* cost                                     # to calculate the down payment of the dream home 

months_of_saving = 0
raise_percent = int(input("What's the semi-annual raise percentage? "))

annual_ret = (int(input("What's the investment annual return? "))/100)

while saving < dwn_pymt:
    saving += (saving * (annual_ret/12))                    # this will add the montly interest of investement into savings
    saving += ((sav_percent/100) * monthly_sal)             # this will add the percent of monthly income that user want to save 
    months_of_saving += 1                                   # to increment the months of saving by 1
    if months_of_saving % 6 == 0:                           # to check for the semi monthly increment
        salary = salary + (salary * (raise_percent/100))
        monthly_sal = salary/12
    #print(round(salary))
    #print("Total no. months {}, Total amount that will be saved {}.".format(months_of_saving,round(saving)))
        
print("\n")
print("You would have to work for {} months to afford the downpayment for your dream house.".format(months_of_saving))

print("{} months or {} years and {} months".format(months_of_saving, (months_of_saving//12), (months_of_saving%12)))

解决方案

100,000), and 8,750/month (base


105,000) for the following six months, and so on…

Sample output 1:

What's your annual salary? 120000
What percent of your salary will you save? 10%
How much does your dream home cost? 1000000
What's the semi-annual raise percentage? 0
What's the investment annual return? 4

You would have to work for 183 months to afford the downpayment for your dream house.
183 months or 15 years and 3 months.
Sample output 2:

What's your annual salary? 150000
What percent of your salary will you save? 10
How much does your dream home cost? 500000
What's the semi-annual raise percentage? 3%
What's the investment annual return? 4%

You would have to work for 75 months to afford the downpayment for your dream house.
75 months or 6 years and 3 months.

Sample output 3:

What's your annual salary? 120000
What percent of your salary will you save? 10%
How much does your dream home cost? 1000000
What's the semi-annual raise percentage? 0
What's the investment annual return? 0%

You would have to work for 250 months to afford the downpayment for your dream house.
250 months or 20 years and 10 months.

Part 2 - Build some savings

You can reuse some of your code from the previous part.

The previous questions focused on how long you would have to work to afford the downpayment. In this question, you’re going to answer the reverse - how much would I have to save to be able to afford the downpayment in X years.

We can iron out some details prior, to make this easier: The semi-annual raise will be 7% The investments have an annual return of 4% The downpayment for the house will be 25% The dream house costs


1m.

For this question, you should find the best rate of savings so that the


这篇关于我在为这个问题的第二部分构建逻辑时遇到了麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆