我有一个python TypeError,我不知道如何解决它

时间:2020-03-10 17:02:57

标签: python

所以我有以下代码:

python setup.py install

这给了我错误“ TypeError:'str'对象不可调用”。我该如何解决?

4 个答案:

答案 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))