The string "end" will never appear in the input. Because of that your code will read past the end of the input and return an error.
You need to stop reading when the input string is "42".
Another thing to consider is that everything that you print will be considered for the solution. Prompts for input will cause a wrong answer. Only output what the problem statement asks for.
I you have any more questions please don't hesitate to post them.