Harvard CS50P PSET 4: TypeError: 'list' object is not callable

Viewed 29

I'm attempting to solve this problem : https://cs50.harvard.edu/python/2022/psets/4/figlet/

However, each time I run it, a TypeError pops up saying "'list' object is not callable". Does anyone know how to fix this?

from pyfiglet import Figlet
import sys
import random
figlet = Figlet()

if len(sys.argv) == 1:
    randomness = True
elif len(sys.argv) == 3 and (sys.argv([1] == "-f" or sys.argv([1]) == "--font")):
    randomness = False
else:
    sys.exit("Invalid Usage")

msg = input("Input:")
random_font = random.choice(figlet.getFonts())

if randomness == True:
    figlet.setFont(font=random_font)
    print(figlet.renderText(msg))

elif randomness == False:
    figlet.setFont(font=sys.argv([2]))
    print(figlet.renderText(msg))
1 Answers

Based on your comment the error is in the elif statement mainly this part:
(sys.argv([1] == "-f" or sys.argv([1]) == "--font"))

sys.argv is a list, but you were calling it with the list parameters. Instead of doing calling it sys.argv([1]) you can directly use it sys.argv[1].
Removing the parentheses would make this:

elif len(sys.argv) == 3 and (sys.argv[1] == "-f" or sys.argv[1] == "--font"):
Related