I'm trying to improve a little program I've made as it's currently got a lot of copied variables with just amended numbers. Is there a possibility to make variables that do a specific calculation where the name of the variable depends on the key and the way it is calculated depends on the value?
Here is a piece of the code:
#Product Retail Price
product_retail_price = float(input("Valor do produto na loja em Reais (Formato 00.00): "))
#Costs
product_cost = float(input("Valor do produto no fornecedor em Reais (Formato 00.00): "))
cpa = float(input("Custo por aquisi??o (Formato 00.00): "))
rrp_divided = product_retail_price / 100
fee_shopify = rrp_divided*2
fee_mercadopago = rrp_divided*4.99
fee_dictionary = {2:2.03,3:4.06,4:6.09,5:7.64,6:8.92,7:10.06,8:10.62,9:11.23,10:12.41,11:13.60,12:14.80,15:18.47,18:22.23,24:23.83}
# for keys, values in fee_dictionary.values():
"""
fee_finance_2 = rrp_divided*2.03
fee_finance_3 = rrp_divided*4.06
fee_finance_4 = rrp_divided*6.09
fee_finance_5 = rrp_divided*7.64
fee_finance_6 = rrp_divided*8.92
fee_finance_7 = rrp_divided*10.06
fee_finance_8 = rrp_divided*10.62
fee_finance_9 = rrp_divided*11.23
fee_finance_10 = rrp_divided*12.41
fee_finance_11 = rrp_divided*13.60
fee_finance_12 = rrp_divided*14.80
fee_finance_15 = rrp_divided*18.47
fee_finance_18 = rrp_divided*22.23
fee_finance_24 = rrp_divided*23.83
"""
What I would like to do is to create multiple variables like the ones at the bottom but without actually copying and pasting them and changing the numbers. I've assigned the key as the number that's supposed to be at the end of each variable, while the value is the number the calculation inside the variable should be divided by.
The latter portion of the code also needs improving and I'd like to use a similar principle. Can any of you suggest the best way?
#Calculations
parcelas = int(input("Número de parcelas sem juros (Formato: 0-24): "))
base_profit = round(product_retail_price - product_cost - fee_shopify - fee_mercadopago - cpa, 2)
profit_2 = round(base_profit - fee_finance_2 - cpa, 2)
profit_3 = round(base_profit - fee_finance_3 - cpa, 2)
profit_4 = round(base_profit - fee_finance_4 - cpa, 2)
profit_5 = round(base_profit - fee_finance_5 - cpa, 2)
profit_6 = round(base_profit - fee_finance_6 - cpa, 2)
profit_7 = round(base_profit - fee_finance_7 - cpa, 2)
profit_8 = round(base_profit - fee_finance_8 - cpa, 2)
profit_9 = round(base_profit - fee_finance_9 - cpa, 2)
profit_10 = round(base_profit - fee_finance_10 - cpa, 2)
profit_11 = round(base_profit - fee_finance_11 - cpa, 2)
profit_12 = round(base_profit - fee_finance_12 - cpa, 2)
profit_15 = round(base_profit - fee_finance_15 - cpa, 2)
profit_18 = round(base_profit - fee_finance_18 - cpa, 2)
profit_24 = round(base_profit - fee_finance_24 - cpa, 2)
#Print Values
print("
")
if parcelas in [0,1]:
print(f"Lucro à vista: R${base_profit}")
elif parcelas == 2:
print(f"Lucro à vista: R${base_profit}")
print(f"Lucro parcelado 2x: R${profit_2}")
elif parcelas == 3:
print(f"Lucro à vista: R${base_profit}")
print(f"Lucro parcelado 2x: R${profit_2}")
print(f"Lucro parcelado 3x: R${profit_3}")
elif parcelas == 4:
print(f"Lucro à vista: R${base_profit}")
print(f"Lucro parcelado 2x: R${profit_2}")
print(f"Lucro parcelado 3x: R${profit_3}")
print(f"Lucro parcelado 4x: R${profit_4}")
elif parcelas == 5:
print(f"Lucro à vista: R${base_profit}")
print(f"Lucro parcelado 2x: R${profit_2}")
print(f"Lucro parcelado 3x: R${profit_3}")
print(f"Lucro parcelado 4x: R${profit_4}")
print(f"Lucro parcelado 5x: R${profit_5}")
elif parcelas == 6:
print(f"Lucro à vista: R${base_profit}")
print(f"Lucro parcelado 2x: R${profit_2}")
print(f"Lucro parcelado 3x: R${profit_3}")
print(f"Lucro parcelado 4x: R${profit_4}")
print(f"Lucro parcelado 5x: R${profit_5}")
print(f"Lucro parcelado 6x: R${profit_6}")
elif parcelas == 7:
print(f"Lucro à vista: R${base_profit}")
print(f"Lucro parcelado 2x: R${profit_2}")
print(f"Lucro parcelado 3x: R${profit_3}")
print(f"Lucro parcelado 4x: R${profit_4}")
print(f"Lucro parcelado 5x: R${profit_5}")
print(f"Lucro parcelado 6x: R${profit_6}")
print(f"Lucro parcelado 7x: R${profit_7}")
elif parcelas == 8:
print(f"Lucro à vista: R${base_profit}")
print(f"Lucro parcelado 2x: R${profit_2}")
print(f"Lucro parcelado 3x: R${profit_3}")
print(f"Lucro parcelado 4x: R${profit_4}")
print(f"Lucro parcelado 5x: R${profit_5}")
print(f"Lucro parcelado 6x: R${profit_6}")
print(f"Lucro parcelado 7x: R${profit_7}")
print(f"Lucro parcelado 8x: R${profit_8}")
elif parcelas == 9:
print(f"Lucro à vista: R${base_profit}")
print(f"Lucro parcelado 2x: R${profit_2}")
print(f"Lucro parcelado 3x: R${profit_3}")
print(f"Lucro parcelado 4x: R${profit_4}")
print(f"Lucro parcelado 5x: R${profit_5}")
print(f"Lucro parcelado 6x: R${profit_6}")
print(f"Lucro parcelado 7x: R${profit_7}")
print(f"Lucro parcelado 8x: R${profit_8}")
print(f"Lucro parcelado 9x: R${profit_9}")
elif parcelas == 10:
print(f"Lucro à vista: R${base_profit}")
print(f"Lucro parcelado 2x: R${profit_2}")
print(f"Lucro parcelado 3x: R${profit_3}")
print(f"Lucro parcelado 4x: R${profit_4}")
print(f"Lucro parcelado 5x: R${profit_5}")
print(f"Lucro parcelado 6x: R${profit_6}")
print(f"Lucro parcelado 7x: R${profit_7}")
print(f"Lucro parcelado 8x: R${profit_8}")
print(f"Lucro parcelado 9x: R${profit_9}")
print(f"Lucro parcelado 10x: R${profit_10}")
elif parcelas == 11:
print(f"Lucro à vista: R${base_profit}")
print(f"Lucro parcelado 2x: R${profit_2}")
print(f"Lucro parcelado 3x: R${profit_3}")
print(f"Lucro parcelado 4x: R${profit_4}")
print(f"Lucro parcelado 5x: R${profit_5}")
print(f"Lucro parcelado 6x: R${profit_6}")
print(f"Lucro parcelado 7x: R${profit_7}")
print(f"Lucro parcelado 8x: R${profit_8}")
print(f"Lucro parcelado 9x: R${profit_9}")
print(f"Lucro parcelado 10x: R${profit_10}")
print(f"Lucro parcelado 11x: R${profit_11}")
elif parcelas in [12,13,14]:
print(f"Lucro à vista: R${base_profit}")
print(f"Lucro parcelado 2x: R${profit_2}")
print(f"Lucro parcelado 3x: R${profit_3}")
print(f"Lucro parcelado 4x: R${profit_4}")
print(f"Lucro parcelado 5x: R${profit_5}")
print(f"Lucro parcelado 6x: R${profit_6}")
print(f"Lucro parcelado 7x: R${profit_7}")
print(f"Lucro parcelado 8x: R${profit_8}")
print(f"Lucro parcelado 9x: R${profit_9}")
print(f"Lucro parcelado 10x: R${profit_10}")
print(f"Lucro parcelado 11x: R${profit_11}")
print(f"Lucro parcelado 12x: R${profit_12}")
elif parcelas in [15,16,17]:
print(f"Lucro à vista: R${base_profit}")
print(f"Lucro parcelado 2x: R${profit_2}")
print(f"Lucro parcelado 3x: R${profit_3}")
print(f"Lucro parcelado 4x: R${profit_4}")
print(f"Lucro parcelado 5x: R${profit_5}")
print(f"Lucro parcelado 6x: R${profit_6}")
print(f"Lucro parcelado 7x: R${profit_7}")
print(f"Lucro parcelado 8x: R${profit_8}")
print(f"Lucro parcelado 9x: R${profit_9}")
print(f"Lucro parcelado 10x: R${profit_10}")
print(f"Lucro parcelado 11x: R${profit_11}")
print(f"Lucro parcelado 12x: R${profit_12}")
print(f"Lucro parcelado 15x: R${profit_15}")
elif parcelas in [18,19,20,21,22,23]:
print(f"Lucro à vista: R${base_profit}")
print(f"Lucro parcelado 2x: R${profit_2}")
print(f"Lucro parcelado 3x: R${profit_3}")
print(f"Lucro parcelado 4x: R${profit_4}")
print(f"Lucro parcelado 5x: R${profit_5}")
print(f"Lucro parcelado 6x: R${profit_6}")
print(f"Lucro parcelado 7x: R${profit_7}")
print(f"Lucro parcelado 8x: R${profit_8}")
print(f"Lucro parcelado 9x: R${profit_9}")
print(f"Lucro parcelado 10x: R${profit_10}")
print(f"Lucro parcelado 11x: R${profit_11}")
print(f"Lucro parcelado 12x: R${profit_12}")
print(f"Lucro parcelado 15x: R${profit_15}")
print(f"Lucro parcelado 18x: R${profit_18}")
else:
print(f"Lucro à vista: R${base_profit}")
print(f"Lucro parcelado 2x: R${profit_2}")
print(f"Lucro parcelado 3x: R${profit_3}")
print(f"Lucro parcelado 4x: R${profit_4}")
print(f"Lucro parcelado 5x: R${profit_5}")
print(f"Lucro parcelado 6x: R${profit_6}")
print(f"Lucro parcelado 7x: R${profit_7}")
print(f"Lucro parcelado 8x: R${profit_8}")
print(f"Lucro parcelado 9x: R${profit_9}")
print(f"Lucro parcelado 10x: R${profit_10}")
print(f"Lucro parcelado 11x: R${profit_11}")
print(f"Lucro parcelado 12x: R${profit_12}")
print(f"Lucro parcelado 15x: R${profit_15}")
print(f"Lucro parcelado 18x: R${profit_18}")
print(f"Lucro parcelado 24x: R${profit_24}")
print("
")
print("AVISO: Lucro calculado usando as taxas atuais do MercadoPago (23/07/2018)")
EDIT:
ERROR
NameError Traceback (most recent call last)
<ipython-input-2-6836971e136f> in <module>()
21
22 profit = {n: round(base_profit - (rrp_divided * fee_dictionary[n]) - cpa, 2)
---> 23 for n in fee_dictionary.keys()}
24
25 print(f"Lucro à vista: R${base_profit}")
<ipython-input-2-6836971e136f> in <dictcomp>(.0)
21
22 profit = {n: round(base_profit - (rrp_divided * fee_dictionary[n]) - cpa, 2)
---> 23 for n in fee_dictionary.keys()}
24
25 print(f"Lucro à vista: R${base_profit}")
NameError: name 'rrp_divided' is not defined
Final version looks much better :D :
#Product Retail Price
product_retail_price = float(input("Valor do produto na loja em Reais (Formato 00.00): "))
#Costs
product_cost = float(input("Valor do produto no fornecedor em Reais (Formato 00.00): "))
cpa = float(input("Custo por aquisi??o (Formato 00.00): "))
fee_shopify = (product_retail_price / 100)*2
fee_mercadopago = (product_retail_price / 100)*4.99
base_profit = round(product_retail_price - product_cost - fee_shopify - fee_mercadopago - cpa, 2)
rrp_divided = (product_retail_price / 100)
parcelas = int(input("Número de parcelas sem juros: "))
while True:
if parcelas in [1,13,14,16,17,19,20,21,22,23]:
print("Número de parcelas incorreto.")
parcelas = int(input("Número de parcelas sem juros: "))
else:
break
fee_dictionary = {2:2.03,3:4.06,4:6.09,5:7.64,6:8.92,7:10.06,8:10.62,9:11.23,10:12.41,11:13.60,12:14.80,15:18.47,18:22.23,24:23.83}
profit = {n: round(base_profit - (rrp_divided * fee_dictionary[n]) - cpa, 2)
for n in fee_dictionary.keys()}
print("
")
print(f"Lucro à vista: R${base_profit}")
for n in range(2, parcelas + 1):
print(f"Lucro parcelado {n}x: R${profit[n]}")
print("
")
print("AVISO: Lucro calculado usando as taxas atuais do MercadoPago (23/07/2018)")
Can I have an empty key in my dictionary? One that simply displays nothing?
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-8-ea0c27a58e66> in <module>()
28 print(f"Lucro à vista: R${base_profit}")
29 for n in range(2, parcelas + 1):
---> 30 print(f"Lucro parcelado {n}x: R${profit[n]}")
31
32 print("
")
KeyError: 13
FINALish VERSION:
#Inputs
product_retail_price = float(input("Valor do produto na loja em Reais (Formato 00.00): "))
product_cost = float(input("Valor do produto no fo