开发者社区> 问答> 正文

Python报错:cannot unpack non-iterable builtin_func

Python报错:

TypeError: cannot unpack non-iterable builtin_function_or_method object

我写了一个方法,去保存未知的变量,它仅适用于一个方程式,当我想基于一个运算符返回值时却无效。

生效的代码:

def solveforx(y):
    x, add, num1, equal, num2 = y.split()

    # convert the strings into ints
    num1, num2 = int(num1), int(num2)
    # convert the result into a string and join it to the string "x = "
    return "x = "+ str(num2 - num1)

print(solveforx("X + 5 = 9"))

报错的代码:

def solveforx(y):
    x, op, num1, equal, num2 = y.split

    num1, num2 = int(num1), int(num2)

    if op == "+":
        return "x = " + str(num2 - num1)
    elif op == "-":
        return "x = " + str(num1 + num2)
    elif op == "*":
        return "x = " + str(num2/num1)
    elif op == "/":
        return "x = " + str(num1*num2)
    else:
        return "wrong operator"

print(solveforx("X + 5 = 9"))

报错信息:

TypeError: cannot unpack non-iterable builtin_function_or_method object

展开
收起
安忆333 2019-12-03 16:55:17 3308 0
1 条回答
写回答
取消 提交回答
  • 你在y.split之后忘记加括号,它应该是

    x, op, num1, equal, num2 = y.split()
    
    2019-12-03 16:56:37
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载