-
-
Save NGWi/9f64f12944e67c9b9a3bbaa30c36bc2f to your computer and use it in GitHub Desktop.
Dataquest Project Lab Walkthrough: Word Raider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
from faker import Faker | |
fake = Faker() | |
# Select random word | |
selected_word = None | |
try: | |
word_bank = open('words.txt', 'a') | |
except FileNotFoundError: | |
word_bank = open('words.txt', 'w') | |
# Attempt to generate a new one: | |
for _ in range(100): | |
word = fake.word() | |
if len(word) == 5: | |
selected_word = word | |
word_bank.write(word + '\n') # Save for another time | |
break | |
# Give up and use one from the word bank | |
else: | |
selected_word = random.choice(word_bank) | |
word_bank.close() | |
game_name = "Word Raider" | |
# Defining game information | |
incorrect_letters = [] | |
misplaced_letters = [] | |
max_turns = 6 | |
used_turns = 0 | |
print(selected_word) | |
print(f"Welcome to {game_name}!") | |
print(f"The word to guess has {len(selected_word)} letters.") | |
print(f"You have {max_turns} turns to guess the word!") | |
while used_turns < max_turns: | |
guess = input("Guess a word: ").lower().strip() | |
if guess == "stop": | |
break | |
if len(guess) != len(selected_word) or not guess.isalpha(): | |
print("Please enter a 5 letter word.") | |
continue | |
output = "" | |
for letter, comparison in zip(guess, selected_word): | |
if letter == comparison: | |
output += letter + " " | |
if letter in misplaced_letters: | |
misplaced_letters.remove(letter) | |
continue | |
elif letter in selected_word: | |
if letter not in misplaced_letters: | |
misplaced_letters.append(letter) | |
else: | |
if letter not in incorrect_letters: | |
incorrect_letters.append(letter) | |
output += "_ " | |
if guess == selected_word: | |
print("\nCongratulations you guessed the word!") | |
break | |
used_turns += 1 | |
if used_turns == max_turns: | |
print(f"Game over, you lost. The word was: {selected_word}") | |
break | |
print() | |
print(output) | |
print("You guessed:", guess) | |
print("Misplaced letters: ", misplaced_letters) | |
print("Incorrect letters: ", incorrect_letters) | |
print(f"You have {max_turns - used_turns} turns left.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment