Home > Software design >  Please help me fix this code so that it can complete the loop and finish the calculations. Im a begi
Please help me fix this code so that it can complete the loop and finish the calculations. Im a begi

Time:05-21

I'm trying to create a calculator to let me know how many waters I need to sell to get my desired profit.

Manly I want this code to calculate How many waters and packs of water I need to make $380 profit.

My code is not returning an eror, but it stops counting at 24 and does not print my f string statments. Please help if you can.


#packs of water 
n = 1

#profit needed
Profit_needed = 380


#Water cost $5 per pack 
Water_Cost = n*5

# 2 packs of ice per pack of water. Ice pack is $2 each 
Ice_cost = (n*2)*2

# number of ice packs 
Ice_amount = Ice_cost*.5

#profit 
Water_Profit = n*25 -Ice_cost - Water_Cost

while Water_Profit < Profit_needed:
    n = n 1
    print(n)
    Ice_cost = (n*2)*2
    Water_Cost = n*5
    Water_Profit = n*25 -Ice_cost - Water_Cost
    Waters_needed = n*25
    Ice_amount = Ice_cost*.5
 
    if Water_Profit == Profit_needed:
        print(f'{n} is cases need')
        print(f' {Water_Profit} is profit')
        print(f'{Waters_needed} is amount needed to sell')
        print(f' {Water_Cost} is the Water cost')
        print(f' {Ice_cost} is the Ice cost')
        print(f' {Ice_amount} is the Ice bags')

CodePudding user response:

Change

if Water_Profit == Profit_needed:

to

if Water_Profit >= Profit_needed:

Water_profit might never exactly equal profit_needed.

Another thing you could do is to move the prints inside the if to outside the while-loop (without the if condition) since the execution will only reach there if Water_Porfit met the condition you wanted.

CodePudding user response:

Your code is excellent, thing there is , at your loop you just pass by the 380$ profit directly to 384$ from somewhere below 380$

and your if statement is == which means equal , now 380 == 384 returns False

so a Correction would be if Water_Profit >= Profit_needed: , if you are interested in the exact amount of 380 , you'll have to add some code to it to deal with float numbers , or find another approach.

#packs of water 
n = 1

#profit needed
Profit_needed = 380


#Water cost $5 per pack 
Water_Cost = n*5

# 2 packs of ice per pack of water. Ice pack is $2 each 
Ice_cost = (n*2)*2

# number of ice packs 
Ice_amount = Ice_cost*.5

#profit 
Water_Profit = n*25 -Ice_cost - Water_Cost

while Water_Profit < Profit_needed:
    n = n 1
    print(n)
    Ice_cost = (n*2)*2
    Water_Cost = n*5
    Water_Profit = n*25 -Ice_cost - Water_Cost
    Waters_needed = n*25
    Ice_amount = Ice_cost*.5

    if Water_Profit >= Profit_needed:
        print(f'{n} is cases need')
        print(f' {Water_Profit} is profit')
        print(f'{Waters_needed} is amount needed to sell')
        print(f' {Water_Cost} is the Water cost')
        print(f' {Ice_cost} is the Ice cost')
        print(f' {Ice_amount} is the Ice bags')
  • Related