"str" object is not callable, In Python classes

Question:

I’m getting this error when I input a or b or c into choice1‘s input:

TypeError: 'str' object is not callable

This is my code:

class a:
    name = "option a"
class b:
    name = "option b"
class c:
    name = "option c"
choice1 = input("input: ")
choice = choice1()
print(choice.name)
Asked By: Yuki

||

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.

Answered By: Broxy
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.