为什么我不能使用 eval() 返回的值再次调用计算器函数并使用答案计算其他值?

Why can't i use the value that eval() returned to call the calculator function again and use the answer calculate something else?

我想在使用计算器对其执行更多操作后计算出的答案,但是当我尝试将答案变量用于 calc() 函数时它 returns 这个错误, TypeError: 'in <string>' requires string as left operand, not function.

我怎样才能拥有它,让我可以不断地获取答案并继续使用它执行更多操作?此外,我无法弄清楚如何不断使用这个计算器而不是在一次计算后才完成的脚本。保留它 运行 直到我不想要它的最佳方式是什么?

# Calculates basic operations
def calc(x, op, y):
    if op in "+-*/":
        ans =  eval(str(x) + op + str(y))
        return ans

# Main function that controls the text-based calculator
def console_calculator():

    def user_input():
        while True:
            x = input('Type your first number: ')
            try:
                return int(x)
            except ValueError:
                try:
                    return float(x)
                except ValueError: 
                    print('Please type in a number...')
        
    def operation_input():
        while True:
            operation = input('Type one of the following, "+ - * /": ')
            if operation in "+-*/":
                return operation
            else:
                print('Please type one of the following, "+ - * /"...')
             

    answer = calc(user_input(), operation_input(), user_input())
    print(answer)

    print(calc(str(answer), operation_input, user_input)) # This line of code throws the error

console_calculator()

when I try using the answer variable into the calc() function it returns this error, "TypeError: 'in ' requires string as left operand, not function".

当您传递不带括号的函数(如参数)时,您传递的是函数对象,而不是函数返回的值。

 print(calc(str(answer), operation_input(), user_input()))