Merged
GuesstheWordGame.py #2662
Changes from all commits
Show all changes
2 commitsSelect commit Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Failed to load files.
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import random | ||
def choose_random_word(): | ||
words = ["apple", "banana", "cherry", "grape", "orange", "watermelon", "kiwi", "mango"] | ||
return random.choice(words) | ||
def display_word(word, guessed_letters): | ||
display = "" | ||
for letter in word: | ||
if letter in guessed_letters: | ||
display += letter | ||
else: | ||
display += "_" | ||
return display | ||
def guess_the_word(): | ||
print("Welcome to Guess the Word game!") | ||
secret_word = choose_random_word() | ||
guessed_letters = [] | ||
attempts = 6 | ||
while attempts > 0: | ||
print(f"\nWord: {display_word(secret_word, guessed_letters)}") | ||
print(f"Attempts left: {attempts}") | ||
guess = input("Guess a letter: ").lower() | ||
if len(guess) != 1 or not guess.isalpha(): | ||
print("Invalid input. Please enter a single letter.") | ||
continue | ||
if guess in guessed_letters: | ||
print("You already guessed that letter.") | ||
continue | ||
guessed_letters.append(guess) | ||
if guess in secret_word: | ||
if set(guessed_letters) == set(secret_word): | ||
print("Congratulations! You guessed the word!") | ||
print(f"The word was: {secret_word}") | ||
Check failureCode scanning / CodeQL Clear-text logging of sensitive information This expression logs [sensitive data (secret)](1) as clear text. | ||
break | ||
else: | ||
print("Correct guess!") | ||
else: | ||
attempts -= 1 | ||
print("Incorrect guess!") | ||
else: | ||
print("Game over! You ran out of attempts.") | ||
print(f"The word was: {secret_word}") | ||
Check failureCode scanning / CodeQL Clear-text logging of sensitive information This expression logs [sensitive data (secret)](1) as clear text. | ||
if __name__ == "__main__": | ||
guess_the_word() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
Package/Script Name: Guess the Word Game | ||
Short Description: This is a simple Python script for playing the "Guess the Word" game. The script randomly selects a word from a predefined list, and the player needs to guess the word by entering letters one by one. The player has a limited number of attempts to guess the complete word correctly. | ||
Functionalities: | ||
Randomly select a word from a predefined list. | ||
Prompt the player to enter letters to guess the word. | ||
Display the progress of the word with guessed letters. | ||
Limit the number of attempts for the player. | ||
End the game with a win or lose message. | ||
Setup Instructions: | ||
Make sure you have Python installed on your system. If you don't have it, you can download it from the official website: https://www.python.org/downloads/ | ||
Copy and paste the provided script into a new file named guess_the_word.py or any desired name. | ||
Save the file in the desired directory. | ||
How to Run the Script: | ||
Open a terminal or command prompt. | ||
Navigate to the directory where you saved the guess_the_word.py file using the cd command. | ||
Run the script by entering the following command: | ||
Copy code | ||
python guess_the_word.py | ||
Detailed Explanation: | ||
The script starts by defining three functions: | ||
choose_random_word: This function selects a random word from a predefined list of words. | ||
display_word: This function takes the secret word and a list of guessed letters as input and returns a string representing the current state of the word with guessed letters displayed and unguessed letters hidden as underscores. | ||
guess_the_word: This function is the main game loop where the user is prompted to enter letters and make guesses until they either guess the word correctly or run out of attempts. | ||
In the guess_the_word function, the game continues in a loop until the player wins or runs out of attempts. The player's input is processed, and the script keeps track of the guessed letters. If the player guesses a letter correctly, it is added to the list of guessed letters, and the script checks if the player has guessed the complete word. If the player guesses a letter incorrectly, the attempts are decremented. If the player either wins or runs out of attempts, the game ends, and the secret word is revealed. | ||
Output: | ||
When running the script, the output will be text-based, displaying the current state of the word, the number of attempts left, and messages for correct and incorrect guesses. If the player wins, the script will display a congratulatory message along with the secret word. If the player loses, the script will show a message indicating that they ran out of attempts and reveal the secret word. | ||
As this is a text-based game, there are no images, gifs, or videos to display for the output. | ||
Author: | ||
Shikhar9425 |
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Clear-text logging of sensitive information