如何在 python 中的方程式中使用用户的输入



我是comp sci的新手,真的需要你的帮助!

我的任务是为一家甜品店编写代码。我们必须创建两个函数,一个用于获取蛋糕的成本,另一个用于获取客户的订单并打印出多少。我的蛋糕成本函数本身运行良好,但是当我尝试运行customer_order函数时,我收到一条错误消息。cake_cost函数应返回赋值中所述的浮点数。这是代码:

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 customer_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 customer_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

我收到一条错误消息,指出"float"对象无法解释为整数。我该如何解决这个问题?如何仅通过调用我的customer_order函数来获取用户输入并将其插入原始函数?有什么办法吗?非常感谢!:)

P.s很抱歉,如果缩进看起来有点奇怪..

恭喜您开始您的 compsci 之旅!您的代码大部分都可以工作,我只需要进行一些修改即可使其完全工作。

  1. 使用传递到cake_cost函数中的参数。在函数参数中将diameter重命名为base_diameter。您还需要在同一函数中将height重命名为height_per_level
  2. get_pizza_area不是定义的函数,请实现它。或者,如果您已经拥有代码,发布它会很有用,这样我们就可以获得更多上下文/帮助您更多。
  3. 代码在第 3 行出错的原因是 range 函数只接受整数。您可以将base_diameter的输入四舍五入为整数,以便可以从 1 循环到 base_diameter + 1。例如,如果base_diameter为 4,则将遍历 [1,2,3,4] 的数组。您可以在此处阅读有关范围函数的更多信息。
  4. 我在评论中添加了全局变量:D
  5. 在代码清洁方面,我们可以压缩以下 4 行:
x = str(input("What diameter? "))
base_diameter = int(x)
y = str(input("What height? "))
height = float(y)

默认情况下,input() 函数接受一个字符串。将字符串强制转换为字符串,然后将其转换为整数或浮点数是没有意义的。相反,您可以执行以下操作:

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 customer_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.”")
customer_order()

如果我运行代码片段,我会得到这样的内容

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

将冲突行更改为

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

并且不要忘记重命名base_diameterdiameterheight_per_levelheight.

范围构造函数的参数必须是整数。 将float函数更改为base_diameter上的int函数。

input()函数返回一个字符串,因此无需将其包装在str()函数中。

相关内容

  • 没有找到相关文章

最新更新