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