Question 17: If Else

Sarah has $5000 saved up for her holiday shopping. She plans to buy gifts for her family and make a few purchases for herself. After calculating her expenses, she wants to divide the remaining amount into two parts: personal shopping and savings.

Here’s how to structure the problem:

  1. Initial Setup:

    • Define a variable for Sarah’s initial savings and set it to $500.
  2. Input Expenses:

    • Ask Sarah how much she spent on a new phone (phone_cost).
    • Ask her how much she spent on clothes (clothes_cost).
    • Ask how much she spent on a book (book_cost).
    • Ask how much she spent on a holiday decoration (decoration_cost).
    • Ask how much she spent on a special dinner (dinner_cost).
  3. Calculate Remaining Balance:

    • Subtract the total expenses (phone_cost, clothes_cost, book_cost, decoration_cost, dinner_cost) from the initial savings.
    • Store the result in a variable called remaining_balance.
  4. Decide Spending and Saving:

    • Ask Sarah how much she wants to allocate for personal shopping (personal_shopping).
    • Subtract this amount from the remaining_balance.
    • The rest should automatically go into savings.
  5. Check Validity:

    • Use an if condition to ensure Sarah’s spending doesn’t exceed her remaining balance.
    • If it does, inform her that she needs to reduce her personal shopping amount.
    • Otherwise, confirm the split and display the final savings amount.
  6. Display Result:

    • Use Streamlit’s st.columns to display the expenses, remaining balance, and the breakdown between personal shopping and savings.
Write what you are looking for, and press enter to begin your search!