Repository navigation
Expand file tree
/
Copy pathquestion_1.py
More file actions
72 lines (65 loc) · 2.46 KB
/
Copy pathquestion_1.py
File metadata and controls
72 lines (65 loc) · 2.46 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
# Question 1: Personal Budget
# Create a budget calculator using a function that tracks income and expenses.
# Requirements:
# Create a function called calculate_budget() that:
# Asks the user for their monthly income
# Asks for 3 expenses: rent, food, and entertainment
# Calculates total expenses and remaining money
# Uses if/elif/else to give budget advice:
# If remaining money < 0: "You're overspending! Cut back on expenses."
# If remaining money < 100: "Your budget is tight. Be careful with spending."
# If remaining money >= 100: "Great job! You have money left over."
# Displays a formatted summary
# Call the function at the end of your program
# Create file: question1.py
# Example Output:
# Enter your monthly income: $3000
# Enter rent expense: $1200
# Enter food expense: $400
# Enter entertainment expense: $300
# === BUDGET SUMMARY ===
# Monthly Income: $3000.00
# Total Expenses: $1900.00
# Remaining Money: $1100.00
# Budget Advice: Great job! You have money left over.
def calculate_budget():
income = input_validation("Enter your monthly income: ")
if income == None:
return
rent = input_validation("Enter rent expense: ")
if rent == None:
return
food = input_validation("Enter food expense: ")
if food == None:
return
entertainment = input_validation("Enter extertainment expense: ")
if entertainment == None:
return
total_expense = rent + food + entertainment
remaining = income - total_expense
print("\n=== BUDGET SUMMARY ===")
print(f"Monthly Income: ${income:.2f}")
print(f"Total Expenses: ${total_expense:.2f}")
print(f"Remaining Money: ${remaining:.2f}")
if remaining < 0:
print("\n Budget Advice: You're overspending! Cut back on expenses.")
elif remaining <100:
print("\n Budget Advice: Your budget is tight. Be careful with spending.")
else:
print("\n Budget Advice: Great job! You have money left over.")# print("The remaining value is : ${.2f}".format(remaining))
def input_validation(display_str):
while True:
try:
input_str = input(display_str).strip()
if input_str[0]=="$":
input_str = input_str[1:]
input_value = float(input_str)
break
except ValueError:
print("Please enter a valide number")
continue
except KeyboardInterrupt:
print("\nExiting...")
return None
return input_value
calculate_budget()