+4 votes
in Programming Languages by (77.2k points)

I am getting the error - "ValueError: substring not found" for the following code.

v = int(ch[2:ch.index('M')])

What is wrong in this code?

1 Answer

+1 vote
by (354k points)
selected by
 
Best answer

There is nothing wrong with your code. It seems that the variable 'ch' does not have character 'M' and hence index() throws: "ValueError: substring not found".

Print the value of the variable 'ch' to check it.

You can add a condition to make your code work.

if 'M' in ch:

v = int(ch[2:ch.index('M')])


...