Library › Coding AI › High school students
Add input checks to your number guessing game
Practice: Setting limits and rules. About 5 minutes.
About this exercise
Your Python guessing game crashes when someone types 'abc'. Fix only get_guess so it keeps asking until it gets a whole number from 1 to 50, printing exactly 'Please type a whole number.' for non-numbers and 'Pick a number from 1 to 50.' for numbers out of range. Use a while True loop with try/except ValueError, no imports and no recursion (you haven't covered it). Return only get_guess in exactly 1 code block of at most 12 lines, with under 40 words of text.
Common mistakes when you ask AI for this
- Getting the whole program back. The AI reprints the full game and may 'improve' play() too. Try: Say: "Return only the get_guess function."
- Not giving the messages. The AI writes its own error messages, which might not match what you want players to see. Try: Paste them: "Print exactly 'Please type a whole number.' and 'Pick a number from 1 to 50.'"
- Code you haven't learned. The AI may call get_guess() again inside itself (recursion) or use extra tools. Try: Add: "Use while True with try/except ValueError. No recursion, no imports."
- A long explanation. The AI explains try/except in detail. Try: Add: "Just the code block, one short sentence at most."
What a strong prompt includes
- Asks for only the function
- Gives the exact messages
- Covers the out-of-range case
- Says what you've learned
- Limits the explanation
Related exercises
- Add a cron line that matches the others in a crontab (Coding AI · Product managers)
- Add dark mode by overriding your existing CSS variables (Coding AI · Software developers)
- Add input validation to a Java method with exact messages (Coding AI · Software developers)
- Add labels to a signup form without rewriting the page (Coding AI · High school students)
- Add mail-merge greetings for couples and missing names (Coding AI · Nonprofit staff)