+4 votes
in Programming Languages by (14.3k 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

0 votes
by (25.0k points)
edited by

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