#1a: annual_salary = float(input('Enter your annual salary: ')) portion_saved = float(input('Enter the percent of your salary to save, as a decimal: ')) total_cost = float(input('Enter the cost of your dream home: ')) current_savings = 0 portion_down_payment = 0.25 down_cost = total_cost * portion_down_payment r = 0.04 monthly_salary = annual_salary/12 num_months = 0 while current_savings < down_cost: current_savings += monthly_salary * portion_saved + current_savings * r/12 num_months += 1 num_years = num_months/12 print('Number of months:', num_months) print('Number of years:',num_years) #1b annual_salary = float(input('Enter your annual salary: ')) portion_saved = float(input('Enter the percent of your salary to save, as a decimal: ')) total_cost = float(input('Enter the cost of your dream home: ')) semi_annual_raise = float(input('Enter the semiannual raise, as a decimal: ')) current_savings = 0 portion_down_payment = 0.25 down_cost = total_cost * portion_down_payment r = 0.04 monthly_salary = annual_salary/12 num_months = 0 while current_savings < down_cost: current_savings += monthly_salary * portion_saved + current_savings * r/12 num_months += 1 if num_months%6 == 0: monthly_salary *= (1 + semi_annual_raise) num_years = num_months/12 print('Number of months:', num_months) print('Number of years:',num_years) #1c def whatrate(): annual_salary = int(input('Enter the starting salary: ')) total_cost = 1000000 semi_annual_raise = 0.07 current_savings = 0 portion_down_payment = 0.25 down_cost = total_cost * portion_down_payment r = 0.04 monthly_salary = annual_salary/12 num_months = 36 high = 10000 low = 0 ans = (high + low)/2 numofsteps = 0 max_savings = 0 for i in range(1,num_months+1): max_savings += monthly_salary + current_savings * r/12 if i%6 == 0: monthly_salary *= (1 + semi_annual_raise) if max_savings < down_cost: return print('It is not possible to pay the down payment in three years.') min_savings = 0 for i in range(1,num_months+1): min_savings += monthly_salary * 0.0001 + current_savings * r/12 if i%6 == 0: monthly_salary *= (1 + semi_annual_raise) if min_savings > down_cost: return print('You don\'t really need to save, mate.') while abs(current_savings - down_cost) > 100: current_savings = 0 monthly_salary = annual_salary/12 numofsteps += 1 for i in range(1,num_months+1): current_savings += monthly_salary * ans/10000 + current_savings * r/12 if i%6 == 0: monthly_salary *= (1 + semi_annual_raise) if current_savings > down_cost: high = ans else: low = ans ans = (high + low)/2 print('Best savings rate:', round(ans/10000,4)) print('Steps in bisection search:', numofsteps)
Lecture 1
Concepts
[Revisit]
Programming
Remaining Question:
u
sa_gold = 46
uk_gold = 27
romania_gold = 1
total_gold = usa_gold + uk_gold + romania_gold
print(total_gold)
romania_gold += 1
total_gold = usa_gold + uk_gold + romania_gold print(total_gold)
My question:
Why do we get 75 for the second print?
My answer:
From John's notes:
# radius = radius +1 is equal to radius += 1
so, romania_gold + = 1 is equal to romania_gold = romania gold + 1
#1a: annual_salary = float(input('Enter your annual salary: ')) portion_saved = float(input('Enter the percent of your salary to save, as a decimal: ')) total_cost = float(input('Enter the cost of your dream home: ')) current_savings = 0 portion_down_payment = 0.25 down_cost = total_cost * portion_down_payment r = 0.04 monthly_salary = annual_salary/12 num_months = 0 while current_savings < down_cost: current_savings += monthly_salary * portion_saved + current_savings * r/12 num_months += 1 num_years = num_months/12 print('Number of months:', num_months) print('Number of years:',num_years) #1b annual_salary = float(input('Enter your annual salary: ')) portion_saved = float(input('Enter the percent of your salary to save, as a decimal: ')) total_cost = float(input('Enter the cost of your dream home: ')) semi_annual_raise = float(input('Enter the semiannual raise, as a decimal: ')) current_savings = 0 portion_down_payment = 0.25 down_cost = total_cost * portion_down_payment r = 0.04 monthly_salary = annual_salary/12 num_months = 0 while current_savings < down_cost: current_savings += monthly_salary * portion_saved + current_savings * r/12 num_months += 1 if num_months%6 == 0: monthly_salary *= (1 + semi_annual_raise) num_years = num_months/12 print('Number of months:', num_months) print('Number of years:',num_years) #1c def whatrate(): annual_salary = int(input('Enter the starting salary: ')) total_cost = 1000000 semi_annual_raise = 0.07 current_savings = 0 portion_down_payment = 0.25 down_cost = total_cost * portion_down_payment r = 0.04 monthly_salary = annual_salary/12 num_months = 36 high = 10000 low = 0 ans = (high + low)/2 numofsteps = 0 max_savings = 0 for i in range(1,num_months+1): max_savings += monthly_salary + current_savings * r/12 if i%6 == 0: monthly_salary *= (1 + semi_annual_raise) if max_savings < down_cost: return print('It is not possible to pay the down payment in three years.') min_savings = 0 for i in range(1,num_months+1): min_savings += monthly_salary * 0.0001 + current_savings * r/12 if i%6 == 0: monthly_salary *= (1 + semi_annual_raise) if min_savings > down_cost: return print('You don\'t really need to save, mate.') while abs(current_savings - down_cost) > 100: current_savings = 0 monthly_salary = annual_salary/12 numofsteps += 1 for i in range(1,num_months+1): current_savings += monthly_salary * ans/10000 + current_savings * r/12 if i%6 == 0: monthly_salary *= (1 + semi_annual_raise) if current_savings > down_cost: high = ans else: low = ans ans = (high + low)/2 print('Best savings rate:', round(ans/10000,4)) print('Steps in bisection search:', numofsteps)
Lecture 1
Concepts
[Revisit]
Programming
Remaining Question:
u
sa_gold = 46
uk_gold = 27
romania_gold = 1
total_gold = usa_gold + uk_gold + romania_gold
print(total_gold)
romania_gold += 1
total_gold = usa_gold + uk_gold + romania_gold print(total_gold)
My question:
Why do we get 75 for the second print?
My answer:
From John's notes:
# radius = radius +1 is equal to radius += 1
so, romania_gold + = 1 is equal to romania_gold = romania gold + 1