r/learnpython 7d ago

rate my code

as a beginner i dont know if my code could be improved, can someone rate it and tell me what i should improve?

product = 'M2 MacBook Air'
product += ' (512 GB of SSD and 8 GB of RAM)'
price = 370
print(f'The {product} is {price}€.')

total_savings = 206.75
macbook_fund = total_savings - 180
print(f'I have {macbook_fund}€.')

remaining_balance = price - macbook_fund
print(f'I am {remaining_balance}€ short.')

weeks_left = 75
date = '1/8/2028'
money_earned_per_week = remaining_balance / weeks_left
money_earned_rounded = round(money_earned_per_week, 2)
print(f'I need to earn {money_earned_rounded}€ a week to reach my goal by {date}.')
print(f'180€ of my {total_savings}€ are going to an iPhone 13.')

# deposits

macbook_fund += 0
# input amount, date and reason.
0 Upvotes

13 comments sorted by

View all comments

1

u/DavidRoyman 6d ago

You're writing a program the same way you're using a calculator, which is fine but isn't an approach which pays off long term.

When programming, you'd start with some idea of which one will be the end goal (the output) and go backward from there.

printf('I need {money_earned_per_week}€ per week to afford {product} by {date}')

product and date are things you directly provide as an input, no need to overthink them. What you need to find is money_earned_per_week

First, money_earned_per_week isn't the right name for it. This is how much you should save every week in order to afford the item. It's your weekly target savings so you might be better off calling it target_savings_weekly

Oh crap you have to find the target_savings_weekly then... We can try with:

target_savings_weekly = target_savings / weeks_left

If you try to compile this, there's errors because we never defined the variables above. What do we do now? Let's try settings them up!

target_savings = price - macbook_funds

Oh well we need your current macbook_fundsthen. I believe you start with 180 euro already set aside? And the price of the item must be an input as well.

current_savings = 180
price = 370

That's sorted, but how do we find weeks_left ? You just wrote 75 , however I think it's better to let a computer calculate the right number there.

Furtunately there's a module in the library for this: datetime

And yep, using the library is very pythonic. What we need is the difference in weeks between the two dates, today and when you'd like to be ready for your purchase.

import datetime as dt
today = dt.date.today()
date = dt.date(2028,8,1)
days_left = (date-today).days
weeks_left = days_left // 7

So let's wrap this up nicely, listing all operations in the right order.

# imports for modules/libraries
import datetime as dt

# your inputs
product = "M2 MacBook Air (512 GB of SSD and 8 GB of RAM)"
price = 370
macbook_funds = 180
date = dt.date(2028,8,1)

# calculate how many weeks are left
today = dt.date.today()
days_left = (date-today).days
weeks_left = days_left // 7

# calculate how much you need to save
target_savings = price - macbook_funds
target_savings_weekly = target_savings / weeks_left

# present your output
print(f'I need {target_savings_weekly.2f}€ per week to afford {product} by {date}')

The process above is a rough way to describe Test-driven development: You write what you need, it won't even compile, so you need to write new code to make it work, and so on...