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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。