S
13

I finally got why my simple calculator app kept crashing...

I was trying to make a basic tip calculator in Python for my friend's cafe in Springfield, and it kept giving me an error every time someone typed in a letter instead of a number. I spent like 3 hours last night staring at the code before I realized I wasn't checking the user's input at all. Has anyone else had a program break because of bad input and found a good way to handle it for a beginner?
3 comments

Log in to join the discussion

Log In
3 Comments
miles72
miles722mo agoMost Upvoted
Remember thinking input validation was just extra work until my cousin's kid broke my budget tool by typing "ten" instead of 10. Now I wrap every user input in a try-except block right from the start. It feels like adding a safety net, and it saves so much headache later. For a tip calculator, you could just keep asking for the bill amount until they give you a real number. That simple loop makes it feel solid.
6
the_fiona
the_fiona2mo ago
Totally get what you mean about that safety net feeling. My friend built a little game for his niece and didn't validate the score input. She typed "a billion" and it crashed hard. He spent an hour fixing it when a simple check would have caught it. Your point about just looping until you get a real number is exactly right. It turns a fragile script into something that can handle real people being people. What's the weirdest input you've ever seen break something, @miles72?
10
caleb262
caleb2622mo ago
Friend of mine built a simple expense tracker and let users enter dates however they wanted. Someone typed "last Tuesday" and the whole thing just threw an error and lost all their data from that week. Now he sanitizes everything before it even touches the database, learned that lesson the hard way.
6