ValueError:int()以10为底的无效文字:“-” m [i] [i] = int(d [i]) [英] ValueError: invalid literal for int() with base 10: '-' m[i][i] = int(d[i])
问题描述
我正在处理代码,以找到最大的求值(忽略优先级),例如
Im working on the code the finding the maximum evaluation (ignoring precedence) such as
2
5+ 3 * 2
-1 + -4 * -3
-1 + -4 * -3
输出
16
40000000000
40000000000
我有以下代码返回以下错误:
I have the following code which returns the following error:
ValueError: invalid literal for int() with base 10: '-' m[i][i] = int(d[i]) File
注意:我浏览了其他类似错误的问题,但这与每个问题都不相同。
Im使用Python 3并完成工作。
Note: I have looked through the other questions with a similar error, but this is different from each of them. Im using Python 3and it docent work.
def get_maximum_value(expression):
expression="".join(expression.split())
op = expression[1:len(expression):2]
d = expression[0:len(expression)+1:2]
n = len(d)
m = [[0 for i in range(n)] for j in range(n)]
M = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
m[i][i] = int(d[i])
M[i][i] = int(d[i])
for s in range(1,n):
for i in range(n-s):
j = i + s
m[i][j], M[i][j] = MaxValue(i,j,op,m,M)
return M[0][n-1]
num=int(input())
for i in range(num):
expression=input()
print (get_maximum_value(expression))
但是,它显示 ValueError:int()以10为底的无效文字:'-'m [i] [i] = int(d [i])
。
我试图将int更改为float,但仍然无法正常工作。
I tried to change the int to float but it still doesnt work.
推荐答案
有一个类似的问题此处,我通过 SethMMorton 使用函数强制将字符串类型的浮点数直接转换为int很有用。试试看。
There is a similar question asked here and I found the answer by SethMMorton useful by using a function to force conversation of float numbers in string type directly to int. Give it a try.
这篇关于ValueError:int()以10为底的无效文字:“-” m [i] [i] = int(d [i])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!