Home > Software design >  Do you know why I keep receiving the error: int too large to convert to float @ line 38?
Do you know why I keep receiving the error: int too large to convert to float @ line 38?

Time:05-21

I'm trying to make a water slaes calculator that asks me what profit I want, How much water is in each case, how many bags of ice I need, and how much is a bag of ice. I then want python to use this information to print the calculations in the if statement. I'm not sure if I'm explaining everything clearly, but please help if you can. Thanks!

#case of water
n = 1

#profit needed
Profit_needed = input("What's the needed profit?: ")


#Asks how many waters per case
Waterpack = input("How much is one Water case?: ")

#Asks how many water bottles per case
Waterpack_amount = input("How many bottles per case?: ")

#Calculates water cost for n cases of water
Water_Cost = n*int(Waterpack)

#Asks how much is each pack of ice 
Ice_pack = input("How much is one pack of ice?: ")

#Asks how many bags of ice is needed per water case
Ice_percase = input("How many bags of ice per water case?: ")

#Calculates the cost of ice for n cases of water 
Ice_cost = int(Ice_pack)*n*Ice_percase 
print(Ice_cost)

#Ice cost $2 per pack so the amount is always = price*.5 
Ice_amount = int(Ice_cost)*.5
print(Ice_amount)

#Calculates profit for n amount of cases
Water_Profit = (n*int(Waterpack_amount)) -(int(Ice_cost)   Water_Cost)
print(Water_Profit)

while Water_Profit < int(Profit_needed):
    n = n 1
    print(n)
    Water_Cost = n*int(Waterpack)
    Ice_cost = int((Ice_pack)*n)*Ice_percase
    Ice_amount = int(Ice_cost)*.5
    print(Ice_cost)
    Water_Cost = n*int(Waterpack)
    Water_Profit = (n*int(Waterpack_amount)) -(int(Ice_cost) - Water_Cost)
    
    #Calculates waters needed to sell for profit_needed
    Waters_needed = n*int(Waterpack_amount)
    continue

if Water_Profit >= int(Profit_needed):
    #subtracts the difference to make Water_Profit == int(Profit_needed)
    Difference_x = Water_Profit - int(Profit_needed)
    Real_Waterprofit = Water_Profit - Difference_x
    Real_Waterneeded = Waters_needed - Difference_x
    
    #Tells me the amount of cases I need for my desired profit
    print(f'Cases of Water needed: {n}')

    #Tells me the profit 
    print(f' Your profit: ${Real_Waterprofit }')
   
    #Tells me the amount of water I need to sell for my desired profit 
    print(f'Amount needed to sell: {Real_Waterneeded}')
    
    #Tells me the cost of water for my desired profit
    print(f' Water cost: ${Water_Cost}')
    
    #Tells me the cost of Ice for my desired profit
    print(f' Ice cost: ${Ice_cost}')

    #Tells me the number of Ice bags for my desired profit
    print(f' number of Ice bags: {Ice_amount}')

CodePudding user response:

input function return string. multiple of string and integer cause mutiplication of string.

Eg : '10' * 5 = "101010101010"

Try to convert string to integer,

#convert string to int
Ice_percase = int(input("How many bags of ice per water case?: ")) 
Ice_cost = int(Ice_pack)*n*Ice_percase
print(Ice_cost)

CodePudding user response:

Your problem is in dealing with input. Specifically, the input function returns strings, so you need to make all those inputs into Integers first (with the int() function) before you do any calculations. Not doing this lead your multiplications to create long strings instead of numbers.

For example: Currently in your code, Ice_pack is actually a string, so at line 32: Ice_cost = int( (Ice_pack)*n )*Ice_percase, if Ice_pack = "10" and n=5, you will get Ice_pack * n = 1010101010 instead of 50, which you can see easily lead to really big numbers that, as you said in the error, can't be handled by Python

The best solution is to wrap input calls in int() so that any inputs immediately get converted to strings, which helps you:

  • not having to repeatedly call int() again and again in your code
  • not having to deal with strings later down the line (and thus not missing any int() calls)

Also, it is recommened to use 0.5 instead of .5 for clearer readability

#packs of water
n = 1

#profit needed
# WRAP `int()` AROUND input() immediately, or use `float()` if you want decimals
Profit_needed = int(input("What's the needed profit?: "))



Waterpack = int(input("How much is one Water case?: "))
Waterpack_amount = int(input("How many bottles per case?: "))

# Waterpack is now already an Integer so you can freely do calculations
# without worrying about conversion
Water_Cost = n * Waterpack


Ice_pack = int(input("How much is one pack of ice?: "))

Ice_percase = int(input("How many bags of ice per water case?: "))
Ice_cost = Ice_pack * n * Ice_percase 
print(Ice_cost)

# Ice cost $2 per pack so the amount is always = price*.5 
Ice_amount = Ice_cost * 0.5
print(Ice_amount)

#profit
Water_Profit = n*Waterpack_amount -(Ice_cost   Water_Cost)
print(Water_Profit)

while Water_Profit < int(Profit_needed):
    n = n   1
    print(n)
    Water_Cost = n * Waterpack
    Ice_cost = Ice_pack * n * Ice_percase
    Ice_amount = Ice_cost * 0.5
    print(Ice_cost)
    Water_Cost = n * Waterpack
    Water_Profit = (n * Waterpack_amount) - (Ice_cost - Water_Cost)
    Waters_needed = n * Waterpack_amount
    # You do not need `continue` at the end of while loop block
    # It auto continues
    # continue

if Water_Profit >= Profit_needed:
    Difference_x = Water_Profit - Profit_needed
    Real_Waterprofit = Water_Profit - Difference_x
    Real_Waterneeded = Waters_needed - Difference_x
    print(f'Cases of Water needed: {n}')
    print(f' Your profit: ${Real_Waterprofit }')
    print(f'Amount needed to sell: {Real_Waterneeded}')
    print(f' Water cost: ${Water_Cost}')
    print(f' Ice cost: ${Ice_cost}')
    print(f' number of Ice bags: {Ice_amount}')
  • Related