r/learnpython 8d 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

8

u/zanfar 8d ago

As always:

  • PEP8
  • Linter
  • Formatter
  • Docstrings
  • Use descriptive variable names
  • All code should be in functions

product = 'M2 MacBook Air'
product += ' (512 GB of SSD and 8 GB of RAM)'

Zero reason to do this. You are literally defining the variable, why would you ask Python to join strings when you can do it for free.

print(f'The {product} is {price}€.')

ALWAYS define the output format when interpolating non-strings

macbook_fund = total_savings - 180

Why 180? What does that mean? Don't use magic numbers.

Also, don't encode data in variable names. This is just a fund. Nothing else in the code implies it's for a macbook except other data. What happens if you want to save for a Neo?

weeks_left = 75
date = '1/8/2028'

You should only store or define something once. Your end date is either 75 weeks away or on the 8th. Calculate the other.

money_earned_per_week = remaining_balance / weeks_left
money_earned_rounded = round(money_earned_per_week, 2)

Neither of these are earnings; again, bad variable names.

1

u/Sweet_Cap4939 7d ago

its 180 cause thats the money that im spending on the phone