+ 1
[Solved] Quit() or Exception in Python when user enters undesired input and why?
Example in code. The code objective is to enter direction and displacement in that direction as multiline input (e.g up 3), and then to calculate the distance covered starting from the origin. Thank you. https://code.sololearn.com/cz7JflUbg6lE/?ref=app
2 Antworten
+ 4
I don't think it makes sense here to end the program if the input is incorrect. Instead I would display a message and ask for input again.
I think the better question here would be whether to do that using if-else or using -try-except.
https://avidpython.com/JUMP_LINK__&&__python__&&__JUMP_LINK-basics/when-to-use-try-except-instead-of-if-else/
If you really want to exit the program using an exception has the advantage of telling you why the program stopped while quit looks cleaner. You could combine both by logging the exception to a file and then using quit to end the program in a controlled and clean way.
+ 2
Simon Sauter Yes, it's the better question and I was hoping for an answer to that, too. I'll start reading it once I post this, thank you.
Edit: replaced quit() with continue to be able to keep asking for input.
Now I'll look up the ethics of elseless ifs, lol.