|
261 | 261 | "# you can use example.txt for practice." |
262 | 262 | ], |
263 | 263 | "id": "a03d6b6716f8ded8" |
| 264 | + }, |
| 265 | + { |
| 266 | + "metadata": {}, |
| 267 | + "cell_type": "markdown", |
| 268 | + "source": [ |
| 269 | + "### practice challenge: word: le guessing game\n", |
| 270 | + "You have just stumbled upon a GENIUS game idea which has totally not been popularized by john wordle and bought by the new york times i PROMISE.\n", |
| 271 | + "So you've got six tries to guess a five letter word. The word will be randomly chosen from a list of five letter words contained in `words.txt`. If a user guesses a letter correctly, display it as green. If a user guesses a letter that is in the incorrect place, display it as yellow. If it's incorrect, display as gray.\n", |
| 272 | + "\n", |
| 273 | + "A few tips:\n", |
| 274 | + "- install the random package\n", |
| 275 | + "- use `string.split(seperator: string)` to split a string based on a seperator (maybe you could use this when reading this words file?)\n", |
| 276 | + "- treat the user input string as an array, so you can use array methods on it\n" |
| 277 | + ], |
| 278 | + "id": "6eb5ccf5bb79fd95" |
| 279 | + }, |
| 280 | + { |
| 281 | + "metadata": { |
| 282 | + "ExecuteTime": { |
| 283 | + "end_time": "2025-09-07T00:37:32.891846Z", |
| 284 | + "start_time": "2025-09-07T00:36:17.366945Z" |
| 285 | + } |
| 286 | + }, |
| 287 | + "cell_type": "code", |
| 288 | + "source": [ |
| 289 | + "#!pip install random\n", |
| 290 | + "import random\n", |
| 291 | + "import time\n", |
| 292 | + "\n", |
| 293 | + "with open(\"words.txt\", \"r\") as f:\n", |
| 294 | + " words = f.read()\n", |
| 295 | + " words = words.split(\"\\n\")\n", |
| 296 | + " the_word = random.choice(words)\n", |
| 297 | + " # the_word = \"apple\" # testing word\n", |
| 298 | + "\n", |
| 299 | + "def evaluate_word(word):\n", |
| 300 | + " status = [0,0,0,0,0] # 0 = incorrect, 1 = wrong place, 2 = correct\n", |
| 301 | + " word = list(word)\n", |
| 302 | + " checker = list(the_word)\n", |
| 303 | + " # check for correctness\n", |
| 304 | + " for i in range(len(word)): # always 5 but hardcoded constants are bad\n", |
| 305 | + " if word[i] == checker[i]:\n", |
| 306 | + " status[i] = 2\n", |
| 307 | + " checker[i] = \"_\" # placeholder to ensure no double-counting\n", |
| 308 | + " # check for wrong place\n", |
| 309 | + " for i in range(len(word)):\n", |
| 310 | + " if word[i] in checker:\n", |
| 311 | + " status[i] = 1\n", |
| 312 | + " checker[checker.index(word[i])] = \"_\"\n", |
| 313 | + "\n", |
| 314 | + " return status\n", |
| 315 | + "\n", |
| 316 | + "guess_amount = 0\n", |
| 317 | + "guesses_words = []\n", |
| 318 | + "while guess_amount < 6:\n", |
| 319 | + " guessed_word = input(\"Guess a word!\").strip()\n", |
| 320 | + " if len(guessed_word) > 5:\n", |
| 321 | + " print(\"Too many letters!\")\n", |
| 322 | + " continue\n", |
| 323 | + " elif len(guessed_word) < 5:\n", |
| 324 | + " print(\"Not enough letters!\")\n", |
| 325 | + " continue\n", |
| 326 | + " elif not guessed_word in words:\n", |
| 327 | + " print(\"That's not a valid word!\")\n", |
| 328 | + " continue\n", |
| 329 | + " guess_amount += 1\n", |
| 330 | + " status_list = evaluate_word(guessed_word)\n", |
| 331 | + " i = 0\n", |
| 332 | + " for code in status_list:\n", |
| 333 | + " if code == 2: # correct!\n", |
| 334 | + " print(\"\\033[97;102m\", flush = True, end = \"\")\n", |
| 335 | + " elif code == 1: # wrong place :/\n", |
| 336 | + " print(\"\\033[97;103m\", flush = True, end = \"\")\n", |
| 337 | + " else: # WRONG\n", |
| 338 | + " print(\"\\033[97;47m\", flush = True, end = \"\")\n", |
| 339 | + " print(guessed_word[i], end = \"\")\n", |
| 340 | + " i += 1\n", |
| 341 | + " time.sleep(0.25)\n", |
| 342 | + " print(\"\\033[0m\", flush = True, end = \"\") # reset colors!\n", |
| 343 | + " print()\n", |
| 344 | + " if guessed_word == the_word:\n", |
| 345 | + " print(\"That's Correct! It took you\", guess_amount, \" guesses to guess the word \", the_word)\n", |
| 346 | + " break\n", |
| 347 | + "\n" |
| 348 | + ], |
| 349 | + "id": "e9ba521bbee23927", |
| 350 | + "outputs": [ |
| 351 | + { |
| 352 | + "name": "stdout", |
| 353 | + "output_type": "stream", |
| 354 | + "text": [ |
| 355 | + "\u001B[97;102ms\u001B[0m\u001B[97;103ml\u001B[0m\u001B[97;47ma\u001B[0m\u001B[97;47mt\u001B[0m\u001B[97;103me\u001B[0m\n", |
| 356 | + "\u001B[97;102ms\u001B[0m\u001B[97;103me\u001B[0m\u001B[97;103ml\u001B[0m\u001B[97;47ml\u001B[0m\u001B[97;47ms\u001B[0m\n" |
| 357 | + ] |
| 358 | + }, |
| 359 | + { |
| 360 | + "ename": "KeyboardInterrupt", |
| 361 | + "evalue": "Interrupted by user", |
| 362 | + "output_type": "error", |
| 363 | + "traceback": [ |
| 364 | + "\u001B[31m---------------------------------------------------------------------------\u001B[39m", |
| 365 | + "\u001B[31mKeyboardInterrupt\u001B[39m Traceback (most recent call last)", |
| 366 | + "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[14]\u001B[39m\u001B[32m, line 31\u001B[39m\n\u001B[32m 29\u001B[39m guesses_words = []\n\u001B[32m 30\u001B[39m \u001B[38;5;28;01mwhile\u001B[39;00m guess_amount < \u001B[32m6\u001B[39m:\n\u001B[32m---> \u001B[39m\u001B[32m31\u001B[39m guessed_word = \u001B[38;5;28;43minput\u001B[39;49m\u001B[43m(\u001B[49m\u001B[33;43m\"\u001B[39;49m\u001B[33;43mGuess a word!\u001B[39;49m\u001B[33;43m\"\u001B[39;49m\u001B[43m)\u001B[49m.strip()\n\u001B[32m 32\u001B[39m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mlen\u001B[39m(guessed_word) > \u001B[32m5\u001B[39m:\n\u001B[32m 33\u001B[39m \u001B[38;5;28mprint\u001B[39m(\u001B[33m\"\u001B[39m\u001B[33mToo many letters!\u001B[39m\u001B[33m\"\u001B[39m)\n", |
| 367 | + "\u001B[36mFile \u001B[39m\u001B[32m~/PycharmProjects/pythonlessons/.venv/lib/python3.13/site-packages/ipykernel/kernelbase.py:1275\u001B[39m, in \u001B[36mKernel.raw_input\u001B[39m\u001B[34m(self, prompt)\u001B[39m\n\u001B[32m 1273\u001B[39m msg = \u001B[33m\"\u001B[39m\u001B[33mraw_input was called, but this frontend does not support input requests.\u001B[39m\u001B[33m\"\u001B[39m\n\u001B[32m 1274\u001B[39m \u001B[38;5;28;01mraise\u001B[39;00m StdinNotImplementedError(msg)\n\u001B[32m-> \u001B[39m\u001B[32m1275\u001B[39m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28;43mself\u001B[39;49m\u001B[43m.\u001B[49m\u001B[43m_input_request\u001B[49m\u001B[43m(\u001B[49m\n\u001B[32m 1276\u001B[39m \u001B[43m \u001B[49m\u001B[38;5;28;43mstr\u001B[39;49m\u001B[43m(\u001B[49m\u001B[43mprompt\u001B[49m\u001B[43m)\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m 1277\u001B[39m \u001B[43m \u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[43m.\u001B[49m\u001B[43m_parent_ident\u001B[49m\u001B[43m[\u001B[49m\u001B[33;43m\"\u001B[39;49m\u001B[33;43mshell\u001B[39;49m\u001B[33;43m\"\u001B[39;49m\u001B[43m]\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m 1278\u001B[39m \u001B[43m \u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[43m.\u001B[49m\u001B[43mget_parent\u001B[49m\u001B[43m(\u001B[49m\u001B[33;43m\"\u001B[39;49m\u001B[33;43mshell\u001B[39;49m\u001B[33;43m\"\u001B[39;49m\u001B[43m)\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m 1279\u001B[39m \u001B[43m \u001B[49m\u001B[43mpassword\u001B[49m\u001B[43m=\u001B[49m\u001B[38;5;28;43;01mFalse\u001B[39;49;00m\u001B[43m,\u001B[49m\n\u001B[32m 1280\u001B[39m \u001B[43m\u001B[49m\u001B[43m)\u001B[49m\n", |
| 368 | + "\u001B[36mFile \u001B[39m\u001B[32m~/PycharmProjects/pythonlessons/.venv/lib/python3.13/site-packages/ipykernel/kernelbase.py:1320\u001B[39m, in \u001B[36mKernel._input_request\u001B[39m\u001B[34m(self, prompt, ident, parent, password)\u001B[39m\n\u001B[32m 1317\u001B[39m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mKeyboardInterrupt\u001B[39;00m:\n\u001B[32m 1318\u001B[39m \u001B[38;5;66;03m# re-raise KeyboardInterrupt, to truncate traceback\u001B[39;00m\n\u001B[32m 1319\u001B[39m msg = \u001B[33m\"\u001B[39m\u001B[33mInterrupted by user\u001B[39m\u001B[33m\"\u001B[39m\n\u001B[32m-> \u001B[39m\u001B[32m1320\u001B[39m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mKeyboardInterrupt\u001B[39;00m(msg) \u001B[38;5;28;01mfrom\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;28;01mNone\u001B[39;00m\n\u001B[32m 1321\u001B[39m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mException\u001B[39;00m:\n\u001B[32m 1322\u001B[39m \u001B[38;5;28mself\u001B[39m.log.warning(\u001B[33m\"\u001B[39m\u001B[33mInvalid Message:\u001B[39m\u001B[33m\"\u001B[39m, exc_info=\u001B[38;5;28;01mTrue\u001B[39;00m)\n", |
| 369 | + "\u001B[31mKeyboardInterrupt\u001B[39m: Interrupted by user" |
| 370 | + ] |
| 371 | + } |
| 372 | + ], |
| 373 | + "execution_count": 14 |
| 374 | + }, |
| 375 | + { |
| 376 | + "metadata": {}, |
| 377 | + "cell_type": "code", |
| 378 | + "outputs": [], |
| 379 | + "execution_count": null, |
| 380 | + "source": "", |
| 381 | + "id": "ce2445f4923f7e21" |
264 | 382 | } |
265 | 383 | ], |
266 | 384 | "metadata": { |
|
0 commit comments