Home > database >  How to use the user’s input in an equation in python
How to use the user’s input in an equation in python

Time:10-06

I’m new to comp sci and really need your help! My assignment is to code for a dessert shop. We had to create two functions, one to get the cost of a cake and the other to get the order of the client and print out how much it will be. My function for the cake cost works perfectly on it’s own, however when I try to run my costumer_order function I get an error message. The cake_cost function should return a float as stated in the assignment. This is the code:

def cake_cost(diameter, height): ###returns cost as a float rounded
   x=0
   for i in range(1, base_diameter   1): ##error message highlights this line
       x  = get_pizza_area(i) #Total Area
   x = x * height_per_level #Volume
   return round(x * CAKE_COST_PER_CM_CUBED,2)#Price where CAKE_COST_PER_CM_CUBED is a global variable of 4.0

#for example, cake_cost(8, 1.0) would be 640.88


def costumer_order(): ##ask for order and prints out cost
   cake_or_cookie = str(input("Would you like the cake or the cookie? "))
   x = str(input("What diameter? "))
   base_diameter = float(x)
   y = str(input("What height? "))
   height = float(y)
   s_ingredient = str(input("Do you want the secret ingredient? "))
   if cake_or_cookie.lower() == 'cake':
       if s_ingredient == "yes" or s_ingredient == "y": 
           return float(round(cake_cost(base_diameter, height)   SECRET_INGREDIENT, 2)) ##where SECRET_INGREDIENT is a global variable of 19.99
       else:
           return float(round(cake_cost(base_diameter, height), 2)) 
   else:
      print(“ew, get out of my store.”)

##for example costumer_orders()
Would you like the cake or cookie? cake
What diameter? 2
What height? 1
Do you want the super secret ingredient? yes
The cost is $35.7

I get an error message saying that ‘float’ object cannot be interpreted as an integer. How do I solve this? How can I get the user input and plug that into the original function simply by calling my costumer_order function? Is there any way? Thank you so much! :) P.s so sorry if the indentations look a little weird..

CodePudding user response:

Congrats on starting your compsci journey! Your code mostly works, I just had to make a few modifications for it to work fully.

  1. The parameters you're passing into the cake_cost function aren't being used. Rename diameter to base_diameter in the function parameters. You'll also need to rename height to height_per_level in the same function.
  2. get_pizza_area isn't a defined function, implement that. Or if you already have the code, it would be useful posting it so we can get some more context / help you out a little more.
  3. The reason why your code is erroring out on line 3 is because the range function only takes in integers. You can round the input of base_diameter to an integer so that you can loop from 1 until base_diameter 1. For example, if base_diameter is 4, you will loop through an array of [1,2,3,4]. You can read a bit more about the range function here.
  4. I added in the global variables you had in your comments :D
  5. In terms of code cleanliness, we could condense the following 4 lines:
   x = str(input("What diameter? "))
   base_diameter = int(x)
   y = str(input("What height? "))
   height = float(y)

The input() function takes in a string by default. There's no point casting a string to a string and then casting it to an integer or a float. Instead, you can do the following:

   base_diameter = int(input("What diameter? "))
   height = float(input("What height? "))
SECRET_INGREDIENT = 19.99
CAKE_COST_PER_CM_CUBED = 4.0
def cake_cost(base_diameter, height_per_level): ###returns cost as a float rounded
   x=0
   for i in range(1, base_diameter   1):
       x  = get_pizza_area(i) #Total Area
   x = x * height_per_level #Volume
   return round(x * CAKE_COST_PER_CM_CUBED,2)#Price where CAKE_COST_PER_CM_CUBED is a global variable of 4.0

#for example, cake_cost(8, 1.0) would be 640.88

def get_pizza_area(i):
    return i

def costumer_order(): ##ask for order and prints out cost
   cake_or_cookie = str(input("Would you like the cake or the cookie? "))
   base_diameter = int(input("What diameter? "))
   height = float(input("What height? "))
   s_ingredient = str(input("Do you want the secret ingredient? "))
   if cake_or_cookie.lower() == 'cake':
       if s_ingredient == "yes" or s_ingredient == "y": 
           print(float(round(cake_cost(base_diameter, height)   SECRET_INGREDIENT, 2))) ##where SECRET_INGREDIENT is a global variable of 19.99
       else:
           print(float(round(cake_cost(base_diameter, height), 2)))
   else:
      print("ew, get out of my store.”")
costumer_order()

If I run the code snippet, I get something like this

Would you like the cake or the cookie? cake
What diameter? 2
What height? 1
Do you want the secret ingredient? yes
31.99

CodePudding user response:

Change the conflicting line to

for i in range(1, int(diameter   1))

and don't forget to rename base_diameter diameter and height_per_level height.

CodePudding user response:

The arguments to the range constructor must be integers. Change the float function to an int function on base_diameter.

The input() function returns a string, so you don't need to wrap it inside the str() function.

  • Related