Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
NeatSquidYT

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.

Recommended Posts

Posted · Original PosterOP

I'm having issue with a python 3 program, here's the program:

 

price = 0toppings = []prices = {"ThinCrust":3,"DeepPan":4,"StuffedCrust":5,"Topping":1}def main():    global price    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.")    toppings.append(input("Topping 1: "))    toppings.append(input("Topping 2: "))    toppings.append(input("Topping 3: "))    toppings.append(input("Topping 4: "))    toppings.append(input("Topping 5: "))    count = 0    for each in toppings:        if toppings(count) > 9:            toppings.pop[count]        else:            price += 1        count +=1    print(toppings)main()

Spoiler

Main PC || CPU - Xeon E3 1231 V3 || Motherboard - MSI Z97 PC Mate || CPU Cooler - Cooler Master Hyper 212 EVO || RAM - 1x8GB HyperX Fury Blue || Graphics card - MSI TwinFrozr V GTX 970 || Storage - 1x BX100 250GB, 1x 1TB WD Blue || PSU - EVGA G2 750W || Case - Corsair 200R

TV PC || CPU - Pentium G3258 @ 4.5GHz 1.2V || Motherboard - MSI H81M-P33 || CPU Cooler - Stock || RAM - 2x4GB random Samsung RAM || Graphics card - Intel HD Graphics (will be R9 390 in a few weeks) || Storage - 1x SanDisk SSD Plus 120GB, 1x Samsung 1.5TB HDD || PSU - Corsair CX600M || Case - None (soon to be Cooler Master Elite 430)

 

Link to post
Share on other sites

wat, are you coding an online pizza-shop?

 

 

(sorry for not useful post but im a python noob....)


My pc:

http://pcpartpicker.com/p/dvcw23 

(Black Glacier)

 

My server:

Dual xeon x5679 processors, 24gb of ECC memory, Nvidia quadro 295 NVS and 48tb of storage.  (z600

Link to post
Share on other sites

I havent used python in a while, but I think you need to replace

if toppings(count) > 9:

with

if toppings[count] > 9:

might be wrong though.


GPU: Gigabyte GTX 970 G1 Gaming CPU: i5-4570 RAM: 2x4gb Crucial Ballistix Sport 1600Mhz Motherboard: ASRock Z87 Extreme3 PSU: EVGA GS 650 CPU cooler: Be quiet! Shadow Rock 2 Case: Define R5 Storage: Crucial MX100 512GB
Link to post
Share on other sites

 

I'm having issue with a python 3 program, here's the program:

price = 0toppings = []prices = {"ThinCrust":3,"DeepPan":4,"StuffedCrust":5,"Topping":1}def main():    global price    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.")    toppings.append(input("Topping 1: "))    toppings.append(input("Topping 2: "))    toppings.append(input("Topping 3: "))    toppings.append(input("Topping 4: "))    toppings.append(input("Topping 5: "))    count = 0    for each in toppings:        if toppings(count) > 9:            toppings.pop[count]        else:            price += 1        count +=1    print(toppings)main()

Whats the error the shell? 


"Use the force Harry" 

                   -Gandalf

Link to post
Share on other sites

I havent used python in a while, but I think you need to replace

if toppings(count) > 9:

with

if toppings[count] > 9:

might be wrong though.

@NeatSquidYT

 

As far as I understand this is correct, toppings(count) would be calling (hence your error message) the method toppings passing through count but that's not what you want.

Also i'd suggest using a range loop rather than a counter


Current laptop: Macbook Air 13" Broadwell Updated - HD 6000
Current Desktop: i5 4690K (4.2GHz) | Gigabyte GTX 960 G1 (1.515 GHz) | Cooler Master V650S | HyperX 8GB 1866 MHz (1x8) | ASRock Z97 Anniversary | Samsung 1TB HDD | SanDisk Ultra II 240GB SSDZalman R1 Peripherals: Ducky Shine MX Red | Razer Naga 2012 | Razer Kraken Pro | Philips 246V 24"

Link to post
Share on other sites
Posted · Best Answer

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.

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now


×