【问题标题】:Lambda function calculator returns function object instead of calculator resultLambda 函数计算器返回函数对象而不是计算器结果
【发布时间】:2020-01-27 18:21:55
【问题描述】:

我使用这个页面来创建 lambda 异常处理程序。 How to catch exceptions using python lambdas

但是当我在我的代码中实现这个时

        #exception handler
        def executeFunction(x, y):
            try:
                z = lambda x,y : int(x) + int(y)
                return z
            except:
                print('Exception caught')

        if str(dzialanie)=='+':
            print("result is: "  + str(executeFunction(input("input 1st number: "), input("input 2nd number: "))))

...

我收到了这条消息:

添加两个数字类型“+” 减去两个数字类型'-' 乘以两个数字类型'*' 将两个数字相除,输入“/” 退出类型 'q' 您要选择哪种操作? + 输入第一个数字:13 输入第二个数字:13 结果是:.executeFunction.. at 0x00000162200C4288> 添加两个数字类型“+” 减去两个数字类型'-' 乘以两个数字类型'*' 将两个数字相除,输入“/” 退出类型 'q' 您要选择哪种操作?

怎么了?

【问题讨论】:

  • 你创建了 lambda 但从不执行它:z(x, y)
  • 你确定你不只是想要z = x + y吗?我不明白您为什么需要使用 lambda 表达式或 def 语句来定义任何函数。
  • 如果您要做的只是打印一条通用消息来确认它,请不要捕获异常。要么处理异常,要么让它不被捕获。
  • 嗯,因为这就是你的代码正在做的事情,定义一个函数(一个 lambda 只是一个函数)然后你返回它。即,你基本上是先做z = lambda x,y : int(x) + int(y) 然后return z,你期待是什么?请注意,如果要将其分配给名称,则不应使用 lambda。这违背了 lambda 的全部目的,即创建一个匿名函数。它明确违反 PEP8 风格指南。只需使用def
  • 也许您想创建一个运算符和函数字典:calc_functions['+'] = lambda x,y : int(x) + int(y) 然后稍后调用它result = executeFunction(calc_functions[dzialanie], input1, input2)) 这样您就可以捕获未找到键或除以零的异常?

标签: python function


【解决方案1】:

我用 Harshal Parekh 的回答解决了这个问题

        excpetion handler and adding        
        def executeAddition(x, y):
            try:
                z = lambda x, y: int(x) + int(y)
                # you need to execute the lambda, like this:
                return z(x, y)
            except:
                print('Exception caught')

        if str(dzialanie)=='+':
            print("result is: "  + str(executeAddition(input("input 1st number: "), input("input 2nd number: "))))

...

【讨论】:

    【解决方案2】:

    我正在使用 lambda 函数添​​加我的整个 python 计算器。 代码中处理的异常。 整个控制台应用程序项目的 Github 存储库是:https://github.com/adamBernat/PythonImperativeProgramming/blob/master/project.py

    #lambda calculator
    def LambdaCalculator():
        quit_l = False
        quit2_l = False
        dzialanie =' '
        while quit_l !=True:
            x=0.0
            y=0.0
            z=0.0
            print("Welcome in calculator")
    
            while quit2_l != True:
                print("\nto add two numbers type '+'")
                print("to subtract two numbers type '-'")
                print("to multiply two numbers type'*'")
                print("to divide two numbers type '/'")
                print("to exit type 'q'")
                dzialanie=(input('\n which operation you want to choose? '))
    
                #excpetion handler and adding        
                def executeAddition(x, y):
                    try:
                        z = lambda x, y: int(x) + int(y)
                        # you need to execute the lambda, like this:
                        return z(x, y)
                    except:
                        print('Exception caught')
    
                if str(dzialanie)=='+':
                    print("result is: "  + str(executeAddition(input("input 1st number: "), input("input 2nd number: "))))
    
    
                #excpetion handler and multiplying        
                def executeMultip(x, y):
                    try:
                        z = lambda x, y: int(x) * int(y)
                        # you need to execute the lambda, like this:
                        return z(x, y)
                    except:
                        print('Exception caught')
    
                if str(dzialanie)=='*':
                    multip = lambda x,y : x * y
                    print("result is: "  + str(executeMultip(input("input 1st number: ")), int(input("input 2nd number: "))))
    
                #excpetion handler and subtracting        
                def executeSubtract(x, y):
                    try:
                        z = lambda x, y: float(x) - float(y)
                        # you need to execute the lambda, like this:
                        return z(x, y)
                    except:
                        print('Exception caught')
    
                if str(dzialanie)=='-':
                    subtract = lambda x,y : x - y
                    print("result is: "  + str(executeSubtract(input("input 1st number: ") , input("input 2nd number: "))))
    
    
                #excpetion handler and dividing        
                def executeDiv(x, y):
                    try:
                        z = lambda x, y: float(x) / float(y)
                        # you need to execute the lambda, like this:
                        return z(x, y)
                    except:
                        print('Exception caught')
    
                if str(dzialanie)=='/':
                    print("result is: "  + str(executeDiv(input("input 1st number: "), input("input 2nd number: "))))
    
    
    
    
                if str(dzialanie)=='q':
                    quit_l = True
                    quit2_l = True
                    print ("\nsubprogram finished, returning to main menu\n")
                    break
                else:
                    x=1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-21
      • 1970-01-01
      • 2021-12-12
      • 2016-09-15
      • 2020-05-20
      • 2016-02-12
      • 2022-12-06
      • 2015-09-28
      相关资源
      最近更新 更多