ValueError: invalid literal for int() with base 10: '-' m[i][i] = int(d[i])

只谈情不闲聊 提交于 2019-12-24 19:36:59

问题


Im working on the code the finding the maximum evaluation (ignoring precedence) such as

2

5+ 3*2

-1 + -4 * -3

Output

16

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

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))

However, it shows ValueError: invalid literal for int() with base 10: '-' m[i][i] = int(d[i]).

I tried to change the int to float but it still doesnt work.


回答1:


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.



来源:https://stackoverflow.com/questions/46518522/valueerror-invalid-literal-for-int-with-base-10-mii-intdi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!