所以我有以下代码:
python setup.py install
这给了我错误“ TypeError:'str'对象不可调用”。我该如何解决?
答案 0 :(得分:6)
我认为这行
final_topping(final_toppings)
应该是
final_toppings.append(final_topping)
另外,print
的返回值为None
,因此final_result
将为None
。
答案 1 :(得分:1)
我将final_toppings
替换为final_toppings_list
,这样更清楚了:
import random
pizza_toppings = ["bacon", "cheese", "chicken", "pepperoni", "olives"]
pizza_sauce = ["alfredo", "marinara", "ranch", "BBQ"]
def pizza(number_of_toppings):
final_sauce = random.choice(pizza_sauce)
final_toppings_list = []
for i in range(number_of_toppings):
final_topping = random.choice(pizza_toppings)
final_toppings_list.append(final_topping)
final_result = "Your pizza with ", final_toppings_list, " with ", final_sauce, " is ready I guess..."
return final_result
print(pizza(2))
答案 2 :(得分:0)
这是因为您正在使用字符串final_topping
作为函数。只需删除该行就可以了。
答案 3 :(得分:0)
如果您只想使用final_topping
列表中的一个元素(在final_toppings
之后),则不必将pizza_toppings
附加到final_result
列表中。
我注释了脚本中的两行,并使用python f-strings以方便的方式格式化final_result
。
import random
pizza_toppings = ["bacon", "cheese", "chicken", "pepperoni", "olives"]
pizza_sauce = ["alfredo", "marinara", "ranch", "BBQ"]
def pizza(number_of_toppings):
final_sauce = random.choice(pizza_sauce)
#final_toppings = []
for i in range(number_of_toppings):
final_topping = random.choice(pizza_toppings)
#final_toppings.append(final_topping)
final_result = (
f"Your pizza with {final_topping} "
f"and {final_sauce} is ready I guess..."
)
return final_result
print(pizza(2))