Python TypeError: 'NoneType' object is not callable

 

I am mere a beginner of Python. I am getting the following error and suspect that it has something to do with the dictionary I've used as switch case (since python does not provide switch ). Following is my code:

class Arithmetic:

    a,b,choice = 0,0,0


    def __init__(self):

        print "\n\n"

        for num in range(30):
            print "*",

        print "\n"    

        print "Welcome to CLC (Command Line Calculator)"

        print "\n"

        for num in range(30):
            print "*",

        print "\n"

    def menu(self):

        print "1. Add"
        print "2. Substract"
        print "3. Multiply"
        print "4. Divide"
        print "5. Modulo"
        print "6. Exit \n\n"

        self.choice = raw_input("Enter Your Choice: ")

        if self.choice == '0':
            exit("Thank you for using the program")

        selector = {
                "1" :   self.add(),
                "2" :   self.substract(), 
                "3" :   self.multiply(),
                "4" :   self.divide(),
                "5" :   self.modulo()
                }

        selector[self.choice]()                    

    def add(self):
        print "Add called"

    def substract(self):
        print "Substract called"

    def multiply(self):
        print "Multiply called"

    def divide(self):
        print "Divide called"

    def modulo(self):
        print "Modulo called"


    def main(self):

        while self.choice != '6':
            self.menu()   



a = Arithmetic()
a.menu() 
Traceback (most recent call last):
 File "arithmetics.py", line 75, in <module>
a.menu()
File "arithmetics.py", line 43, in menu
  selector[self.choice]()                    
TypeError: 'NoneType' object is not callable

The Error is:

When you do

self.add()

you are calling the method (you will get a result). If you want to specify the method, delete the ():

selector = {
            "1" :   self.add,
            "2" :   self.substract, 
            "3" :   self.multiply,
            "4" :   self.divide,
            "5" :   self.modulo
            }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值