Skip to content

Instantly share code, notes, and snippets.

@han-tun
Forked from acstrahl/step_by_step_loop.ipynb
Created March 27, 2025 17:17
Show Gist options
  • Save han-tun/8a08ba59cdaa10045a99360b68f846c4 to your computer and use it in GitHub Desktop.
Save han-tun/8a08ba59cdaa10045a99360b68f846c4 to your computer and use it in GitHub Desktop.
Dataquest Project Lab Walkthrough: Word Raider
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Word Raider"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"game_name = \"Word Raider\"\n",
"word_bank = []"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"with open(\"words.txt\") as word_file:\n",
" for line in word_file:\n",
" word_bank.append(line.rstrip().lower())"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'queen'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"selected_word = random.choice(word_bank)\n",
"selected_word"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"# Defining game information\n",
"incorrect_letters = []\n",
"misplaced_letters = []\n",
"max_turns = 6\n",
"used_turns = 0"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to Word Raider!\n",
"The word to guess has 5 letters.\n",
"You have 6 turns to guess the word!\n"
]
}
],
"source": [
"print(f\"Welcome to {game_name}!\")\n",
"print(f\"The word to guess has {len(selected_word)} letters.\")\n",
"print(f\"You have {max_turns} turns to guess the word!\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Guess a word: kings\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"_ _ _ _ _ \n",
"\n",
"Misplaced letters: ['n']\n",
"Incorrect letters: ['k', 'i', 'g', 's']\n",
"You have 4 turns left.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Guess a word: turny\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"_ u _ _ _ \n",
"\n",
"Misplaced letters: ['n']\n",
"Incorrect letters: ['k', 'i', 'g', 's', 't', 'r', 'y']\n",
"You have 3 turns left.\n"
]
}
],
"source": [
"while used_turns < max_turns:\n",
" guess = input(\"Guess a word: \").lower().strip()\n",
" if guess == \"stop\":\n",
" break\n",
" if len(guess) != len(selected_word) or not guess.isalpha():\n",
" print(\"Please enter a 5 letter word.\")\n",
" continue\n",
" \n",
" index = 0\n",
" for letter in guess:\n",
" if letter == selected_word[index]:\n",
" print(letter, end=' ')\n",
" if letter in misplaced_letters:\n",
" misplaced_letters.remove(letter)\n",
" elif letter in selected_word:\n",
" if letter not in misplaced_letters:\n",
" misplaced_letters.append(letter)\n",
" print(\"_\", end=' ')\n",
" else:\n",
" if letter not in incorrect_letters:\n",
" incorrect_letters.append(letter)\n",
" print(\"_\", end=' ')\n",
" index += 1\n",
" \n",
" if guess == selected_word:\n",
" print(\"\\nCongratulations you guessed the word!\")\n",
" break\n",
" \n",
" used_turns += 1\n",
" \n",
" if used_turns == max_turns:\n",
" print(f\"Game over, you lost. The word was: {selected_word}\")\n",
" break\n",
" \n",
" print(\"\\n\")\n",
" print(\"Misplaced letters: \", misplaced_letters)\n",
" print(\"Incorrect letters: \", incorrect_letters)\n",
" print(f\"You have {max_turns - used_turns} turns left.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment