Python 3 "TypeError: 'list' object is not callable"
Toxicable and ICantThinkOfAnyGoodName are right about where the problem is--toppings(count) tries to call a function toppings with the argument count, but Python finds that toppings is a list, so it break.
But there's another problem I'm seeing--Python 3 doesn't implicitly convert from strings to integers/floats, and the input() function returns a string. So you'll be trying to compare, say, '5' (string) to 9 (int), and it won't work. It won't work anyways when you get to the user-input 'fin' if present.
Here's my quick-and-dirty solution. What I changed:
- The user is now prompted for the toppings via a for-loop. The loop tries to convert the input to an integer and append it to toppings. If it can't do this (i.e., if Python throws a ValueError), then it breaks the loop and stops prompting the user for toppings. This lets the user type "fin" or any non-numeric character to finish entering toppings.
- Using list comprehension, all values outside of the range 0-9 (inclusive) are removed from the list.
- price is incremented by the length of the toppings list, which should be equivalent to what you were doing before.
- toppings is now declared as a global variable, just to be consistent with price, and because I occasionally got some erorrs otherwise.
Note that this doesn't check to make sure the user has a valid topping selection--they can enter "400" and the script won't notice. It will just filter that out of the list later on. You could definitely add some more code to make sure the inputs are valid, though this might complicate the script a bit more.
prices = {"ThinCrust":3,"DeepPan":4,"StuffedCrust":5,"Topping":1}toppings = []price = 0def main(): global price global toppings print("The toppings are: Extra Cheese (0), Salami (1), Ham (2), Mushroom (3), Pineapple (4), Chicken (5), Peppers (6), Onion (7), Bacon (8) and Olives (9)") print("Enter the customer's toppings. If they have said less than 5, then type 'fin' instead of the topping number.") # Input loop--stops prompting the user for input when they enter a # non-numeric character. for XNUM in range(5): uinput = input("Topping %i: " %(XNUM+1)) try: toppings.append(int(uinput)) except ValueError: break toppings = [I for I in toppings if I in range(10)] price += len(toppings) print(toppings)main()
Hopefully this helps. Let me know if it's not working or some other such issue comes up.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now