"str" object is not callable, In Python classes

Viewed 37

Im Getting Error:

im getting an error when i input a or b or c in choice1's input

getting error:

TypeError: 'str' object is not callable

class a:
    name = "option a"
class b:
    name = "option b"
class c:
    name = "option c"
choice1 = input("input: ")
choice = choice1()
print(choice.name)
1 Answers

Like matszwecja said your input is a string and not callable. Also you are trying to execute a string with class.name in your class a which is not possible. If you really want to use classes it would look like this.

class a:
    def name(self):
        print("option a")
class b:
    def name(self):
        print("option a")
class c:
    def name(self):
        print("option a")

choice1 = input("input: ")
if choice1 == "a":
    class_a = a()
    class_a.name()

However you should use if-statements and/or functions.

Related