Ethan Elhart
Ethan Elhart

Reputation: 27

I have a python TypeError and I don't know how to fix it

So I have the following code:

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_topping(final_toppings)
  final_result = print("Your pizza with ", final_topping, " with ", final_sauce, " is ready I guess...")
  return final_result
pizza(2)

It's giving me the error "TypeError: 'str' object not callable." How do I fix this?

Upvotes: 0

Views: 64

Answers (4)

Youssef
Youssef

Reputation: 1495

You don't necessarily need to append final_topping to a final_toppings list if you want to use only one element of your pizza_toppings list (later in final_result).

I commented the two lines in your script and used python f-strings to format final_result in a convenient way..

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

Upvotes: 0

Phoenixo
Phoenixo

Reputation: 2113

I replaced final_toppings by final_toppings_list so it makes it clearer :

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

Upvotes: 1

martin
martin

Reputation: 885

It´s because you´re using the string final_topping as a function. Just delete that line and you´re good.

Upvotes: 0

Cory Kramer
Cory Kramer

Reputation: 117856

I assume this line

final_topping(final_toppings)

should be

final_toppings.append(final_topping)

Also the return of print is None, so final_result will be None.

Upvotes: 6

Related Questions