Concept Explanation
This lesson turns a common beginner problem into a useful debugging exercise. You will start with a small Kotlin program that accepts text input and needs to handle invalid numbers safely. The focus is on reading the code carefully, spotting where things can go wrong, and improving the result without making the solution complicated. It is also a gentle introduction to Kotlin's safety mindset: write code that fails clearly, or better yet, avoids unnecessary failure in the first place.
Where to Put the Code
- Define color and position variables at the top.
- Create shape drawing or placement logic in the middle.
- Render output (print, canvas, SVG, or styled block) at the end.
Command Reference
- Run the starter code and compare the valid path with the invalid path.
- Replace one input value and explain what changed in the output.
- Improve the fallback message so it is clearer for a real user.
- Explain why a safe conversion is often better than forcing a crash.
Step-by-step Guide
- Run the example and inspect both outputs carefully.
- Identify which line prevents the program from crashing on bad input.
- Change the invalid sample and test another edge case.
- Refactor the error message so it gives more useful feedback.
- Write one sentence summarizing how Kotlin helps here.
Practice Exercises
- Add a third test input such as an empty string or a negative value.
- Return a clearer message for invalid data while keeping the code compact.
- Build a second example that validates age, score, or quantity instead of a generic number.
Coding Challenges
- Handle multiple bad inputs cleanly without adding unnecessary complexity.
- Compare a safe nullable approach with a try/catch approach and decide which reads better here.
Mini Practice Tasks
- Add one extra edge-case input and rerun the program.
- Improve one variable name so the intent is immediately obvious.
- Summarize in one line what problem this snippet solves.
Common Mistake
Mixing x and y axes or using wrong coordinate origin causes shapes to appear in unexpected places.
Real-life Mini Challenge
Draw one square, one triangle, and one circle, then move X marker 2 steps right and 1 step down.