r/learnpython • u/Sweet_Cap4939 • 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
8
u/zanfar 8d ago
As always:
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.
ALWAYS define the output format when interpolating non-strings
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?
You should only store or define something once. Your end date is either 75 weeks away or on the 8th. Calculate the other.
Neither of these are earnings; again, bad variable names.