{"id":"code-hs-python-guess-input-validation","audience":"high-school","category":"coding","track":"coding","technique":"constraints","title":"Add input checks to your number guessing game","free":false,"task":"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.","materialLabel":"Your game code and the error (attached to your prompt automatically)","material":"# guess.py  - my project for Intro to Programming (Ms. Kowalski)\nimport random\n\ndef get_guess():\n    guess = int(input(\"Your guess: \"))\n    return guess\n\ndef play():\n    secret = random.randint(1, 50)\n    tries = 0\n    print(\"I'm thinking of a number from 1 to 50.\")\n    while True:\n        guess = get_guess()\n        tries += 1\n        if guess < secret:\n            print(\"Too low!\")\n        elif guess > secret:\n            print(\"Too high!\")\n        else:\n            print(f\"You got it in {tries} tries!\")\n            break\n\nplay()\n\n# What happened when my little brother played:\n#   I'm thinking of a number from 1 to 50.\n#   Your guess: 20\n#   Too low!\n#   Your guess: abc\n#   Traceback (most recent call last):\n#     File \"guess.py\", line 22, in <module>\n#       play()\n#     File \"guess.py\", line 13, in play\n#       guess = get_guess()\n#     File \"guess.py\", line 5, in get_guess\n#       guess = int(input(\"Your guess: \"))\n#   ValueError: invalid literal for int() with base 10: 'abc'\n#\n# Also he typed 500 and it just said \"Too high!\" which counted as a try.","rules":[{"type":"codeBlocks","count":1},{"type":"maxCodeLines","max":12},{"type":"maxProseWords","max":40}],"mistakes":[{"title":"Getting the whole program back","why":"The AI reprints the full game and may 'improve' play() too.","fix":"Say: \"Return only the get_guess function.\"","checks":["r2","maxCodeLines"]},{"title":"Not giving the messages","why":"The AI writes its own error messages, which might not match what you want players to see.","fix":"Paste them: \"Print exactly 'Please type a whole number.' and 'Pick a number from 1 to 50.'\"","checks":["r1"]},{"title":"Code you haven't learned","why":"The AI may call get_guess() again inside itself (recursion) or use extra tools.","fix":"Add: \"Use while True with try/except ValueError. No recursion, no imports.\"","checks":["r0"]},{"title":"A long explanation","why":"The AI explains try/except in detail.","fix":"Add: \"Just the code block, one short sentence at most.\"","checks":["codeBlocks","maxProseWords"]}],"promptHints":[{"label":"Asks for only the function","patterns":["only.{0,15}get_guess","just the (function|get_guess)"]},{"label":"Gives the exact messages","patterns":["whole number","1 to 50"]},{"label":"Covers the out-of-range case","patterns":["range","500","between 1 and 50","1 to 50"]},{"label":"Says what you've learned","patterns":["while true","try","no recursion","haven'?t learned"]},{"label":"Limits the explanation","patterns":["no explanation","only the code","short"]}],"hint":"'Fix my game' could mean anything. Say what should happen with bad input, the exact messages, what you've learned so far, and that you only want the one function back.","rubricLabels":["Uses a while True loop with try/except ValueError around int(input(...)), with no recursion, no imports and no other functions","Prints exactly 'Please type a whole number.' when the input isn't a number and exactly 'Pick a number from 1 to 50.' when it is below 1 or above 50, then asks again","Only get_guess is returned, keeping the name, no parameters, the prompt text 'Your guess: ' and returning the valid number"]}