1
I tried to make a simple game with Python and it crashed in a weird way
I was following a guide to make a number guessing game and used 'input()' to get the player's guess. When I typed a word instead of a number, the whole thing just stopped with a big error. I learned that 'input()' always gives you text, so you have to use 'int()' to change it to a number, but that can break. How do you guys handle it when someone types the wrong thing into your programs?
2 comments
Log in to join the discussion
Log In2 Comments
adam65221d agoMost Upvoted
Ugh, been there! That exact thing messed me up when I was starting. I just wrap the int() conversion in a try and except block now. It lets you catch the error and tell the user to type a number instead of crashing. You can even use a loop to keep asking until they get it right. It's a bit more code but it makes your program way less fragile.
6
henderson.mason21d ago
Man, that exact error drove me crazy on my first project. I spent like two hours trying to figure out why my quiz game would just die if someone typed "yes" instead of "1". @adam652 is totally right about the try and except block. I made a little function that just loops and asks again, like "please enter a number, not text". It feels like a real program after you fix that.
2