在WHILE循环中的FOR循环之前重置并创建变量
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在WHILE循环中的FOR循环之前重置并创建变量相关的知识,希望对你有一定的参考价值。
[我遇到了一个MIT开源Python编码实践
假设您希望能够在三年内支付首付。您每个月应节省多少费用以实现此目标?在这个问题中,您将要编写一个程序来回答该问题。为简化起见,假设:1。您的半年加薪为.07(7%)2。您的投资的年收益为0.04(4%)3。预付款是房屋成本的0.25(25%)。4。您要节省的房屋成本为$ 1M。
您现在将尝试找到最佳的储蓄率,以在36个月内实现首付100万美元房屋的首付。由于要做到这一点确实是一个挑战,因此我们只希望您的存款在所需的首付中不超过100美元。编写一个程序,根据您的起薪来计算最佳储蓄率。您应该使用[二等分搜索]来帮助您有效地做到这一点。您应该跟踪对分搜索完成所需的步骤数。限制浮点数的精度为两位小数(即,我们可能希望以7.04%的价格保存-或以0.0704的十进制保存-但我们不必担心7.041%和7.039%之间的差额)。这意味着我们可以搜索0到10000之间的整数(使用整数除法),然后将其转换为十进制百分比(使用浮点除法),以便在36个月后计算current_savings时使用。使用此范围只会给我们要搜索的有限数量的数字,而不是0到1之间的无穷小数。此范围将有助于防止无限循环。我们使用0到10000的原因是要考虑两个额外的小数位,范围是0%到100%。您的代码应打印出小数点(例如0.0704表示7.04%)。请记住,可能无法在一年半的时间里节省一些工资的预付款。在这种情况下,您的功能应通知用户使用打印报表在36个月内无法节省定金。
示例输出输入起薪:150000最佳储蓄率:0.4411对分搜索的步骤:12
以下是我找到的解决方案之一。
# user input
annual_salary = float(input('Enter your annual salary: '))
# static variables and initializers
semi_annual_raise = 0.07
r = 0.04
portion_down_payment = 0.25
total_cost = 1000000
steps = 0
current_savings = 0
low = 0
high = 10000
guess_rate = (high + low)//2
# Use a while loop since we check UNTIL something happens.
while abs(current_savings - total_cost*portion_down_payment) >= 100:
# Reset current_savings at the beginning of the loop
current_savings = 0
# Create a new variable for use within the for loop.
for_annual_salary = annual_salary
# convert guess_rate into a float
rate = guess_rate/10000
# Since we have a finite number of months, use a for loop to calculate
# amount saved in that time.
for month in range(36):
# With indexing starting a zero, we need to calculate at the beginning
# of the loop.
if month % 6 == 0 and month > 0:
for_annual_salary += for_annual_salary*semi_annual_raise
# Set monthly_salary inside loop where annual_salary is modified
monthly_salary = for_annual_salary/12
# Calculate current savings
current_savings += monthly_salary*rate+current_savings*r/12
# The statement that makes this a bisection search
if current_savings < total_cost*portion_down_payment:
low = guess_rate
else:
high = guess_rate
guess_rate = (high + low)//2
steps += 1
# The max amount of guesses needed is log base 2 of 10000 which is slightly
# above 13. Once it gets to the 14th guess it breaks out of the while loop.
if steps > 13:
break
# output
if steps > 13:
print('It is not possible to pay the down payment in three years.')
else:
print('Best savings rate:', rate)
print('Steps in bisection search:', steps)
为什么需要在FOR循环之前重置变量值current_savings并创建for_annual_salary?当开始时current_ Savings已被定义为0,为什么它在FOR循环中创建一个崭新的变量for_annual_salary而不是使用Annual_salary?
如果您没有为变量分配Annual_salary,并且循环更改了该变量的值,则可以访问用户输入的先前值。例如:
annual_salary = input("Enter salary")
annual_salary = 10000 + 1000
print(annual_salary ) #would give you 11000, And you got some bug in your code and you want to debug what user entered the salary.
You print("annual_salary") #And you get updated salary not what user entered.
如果您创建了如下所示的变量:
annual_salary = input("Enter salary")
new_annual_salary = 10000 + 1000 # some operations
print(new_annual_salary ) #Every operation was performed on new variable and if code goes wrong somewhere,you still can find new and old value of annual_salary.
print(annual_salary)
- 与当前储蓄相同
并且您需要将这些变量初始设置为0,因为在执行操作时,您需要提供一些数字/初始值。
c = 0
for i in range(3):
c+= i
print(c) # You will get 3 not 0
d #If you don't initialize your value and performing operations you'll get undefined error
d+= 1
print(d)
NameError:未定义名称'd'
以上是关于在WHILE循环中的FOR循环之前重置并创建变量的主要内容,如果未能解决你的问题,请参考以下文章