-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem1.py
More file actions
91 lines (66 loc) · 2.81 KB
/
Copy pathproblem1.py
File metadata and controls
91 lines (66 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
problem = "problem1"
student_name = "Abudi Alshamam"
student_numer = "N1212353"
total_cost_of_house = 0
proportion_saved = 0
annual_salary = 0
monthly_salary = 0
annual_savings = 0
is_affordable = False
proportion_down_payment = 0.25
#function for Part A
def months_to_DP(total_cost_of_house, annual_salary, proportion_saved):
annual_salary = float(input("Enter your annual salary: "))
proportion_saved = float(input("Enter the percent of your salary to save, as a decimal: "))
total_cost_of_house = float(input("Enter the cost of your dream home: "))
current_savings = 0
down_payment = total_cost_of_house * proportion_down_payment
months = 0
monthly_salary = (annual_salary / 12) * (proportion_saved / 100)
while current_savings < down_payment:
current_savings += current_savings * (0.05 / 12)
current_savings += monthly_salary
months += 1 # Increment months
print("Number of months: ", months)
return months
#function for Part B
def months_with_biannual_raise(total_cost_of_house, annual_salary, proportion_saved, biannual_raise):
annual_salary = float(input("Enter your annual salary: "))
proportion_saved = float(input("Enter the percent of your salary to save, as a decimal: "))
total_cost_of_house = float(input("Enter the cost of your dream home: "))
current_savings = 0
down_payment = total_cost_of_house * proportion_down_payment
months = 0
biannual_raise = 0.05 # Biannual raise percentage
monthly_salary = (annual_salary / 12) * (proportion_saved / 100)
while current_savings < down_payment:
current_savings += current_savings * (0.04 / 12)
current_savings += monthly_salary
months += 1 # Increment months
if months % 6 == 0: # Apply biannual raise every 6 months
annual_salary += annual_salary * biannual_raise
monthly_salary = (annual_salary / 12) * (proportion_saved / 100)
print("Number of months: ", months)
return months
#function for Part C
def bisection_search():
print("Think of an integer between 1 and 10,000.")
low = 1
high = 10000
guesses = 0
while low <= high:
guesses += 1
guess = (low + high) // 2
print(f"Is your number {guess}?")
response = input("Enter 'c' if correct, 'h' if your number is higher, or 'l' if your number is lower: ").lower()
if response == 'c':
print(f"Guessed your number {guess} in {guesses} guesses!")
return
elif response == 'h':
low = guess + 1
elif response == 'l':
high = guess - 1
else:
print("Invalid input. Please enter 'c', 'h', or 'l'.")
print("Couldn't find your number. Are you sure you followed the rules?")
months_to_DP(total_cost_of_house, annual_salary, proportion_saved)