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
{
"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 = []\n",
"\n",
"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": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'kings'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"selected_word = random.choice(word_bank)\n",
"selected_word"
]
},
{
"cell_type": "code",
"execution_count": 4,
"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": 5,
"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": 6,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Guess a word (or type 'stop' to end game): stop\n"
]
}
],
"source": [
"# Set up a loop with an exit condition\n",
"while used_turns < max_turns:\n",
" guess = input(\"Guess a word (or type 'stop' to end game):\")\n",
" \n",
" if guess == \"stop\":\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Guess a word (or type 'stop' to end game): stop\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"stop\n"
]
}
],
"source": [
"# Verify user input\n",
"while used_turns < max_turns:\n",
" guess = input(\"Guess a word (or type 'stop' to end game):\").lower().strip()\n",
" print(guess)\n",
" \n",
" if guess == \"stop\":\n",
" break\n",
" \n",
" if len(guess) != len(selected_word) or not guess.isalpha():\n",
" print(\"Please enter a 5 letter word.\")\n",
" continue"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Guess a word (or type 'stop' to end game): stop\n"
]
}
],
"source": [
"# Initial loop through characters in guess\n",
"while used_turns < max_turns:\n",
" guess = input(\"Guess a word (or type 'stop' to end game):\").lower().strip()\n",
" \n",
" if guess == \"stop\":\n",
" break\n",
" \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",
" \n",
" for letter in guess:\n",
" if letter == selected_word[index]:\n",
" print(letter)\n",
" elif letter in selected_word:\n",
" misplaced_letters.append(letter)\n",
" print(\"_\")\n",
" else:\n",
" incorrect_letters.append(letter)\n",
" print(\"_\")\n",
" index += 1\n",
" \n",
" print(f\"Misplaced letters: {misplaced_letters}\")\n",
" print(f\"Incorrect letters: {incorrect_letters}\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Guess a word (or type 'stop' to end game): stop\n"
]
}
],
"source": [
"# Accounting for duplicates in misplaced/incorrect letter lists\n",
"while used_turns < max_turns:\n",
" guess = input(\"Guess a word (or type 'stop' to end game):\").lower().strip()\n",
" \n",
" if guess == \"stop\":\n",
" break\n",
" \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",
" \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",
" print(\"\\n\")\n",
" print(f\"Misplaced letters: {misplaced_letters}\")\n",
" print(f\"Incorrect letters: {incorrect_letters}\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Guess a word (or type 'stop' to end game): stop\n"
]
}
],
"source": [
"# Checking for win/loss conditions\n",
"\n",
"# Accounting for duplicates in misplaced/incorrect letter lists\n",
"while used_turns < max_turns:\n",
" guess = input(\"Guess a word (or type 'stop' to end game):\").lower().strip()\n",
" \n",
" if guess == \"stop\":\n",
" break\n",
" \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",
" \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",
" print(\"\\n\")\n",
" \n",
" if guess == selected_word:\n",
" print(\"Congratulations 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(f\"Misplaced letters: {misplaced_letters}\")\n",
" print(f\"Incorrect letters: {incorrect_letters}\")\n",
" print(f\"You have {max_turns - used_turns} turns left.\")"
]
}
],
"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
}
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment