Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python word guessing game #38

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions Algorithams/Python/wordGuessing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@


from random import choice

WRONG_LOSE = 6


def hideLetters(answer, guesses):
"""
Hides unguessed letters in the answer with '*', leaves punctuation and spaces visible.

Returns:
str: The answer with unguessed letters hidden.
"""
result = ""
for char in answer:
if char.upper() in guesses.upper() or not char.isalpha():
result += char # Show the letter or punctuation
else:
result += "*" # Hide the letter
return result


def askPlayer(clue, display, guesses, wrong):
"""
Asks the player for a letter and ensures valid input.

Returns:
str: A valid letter guessed by the player.
"""
while True:
print(f"Clue: {clue}")
print(f"Puzzle: {display}")
print(f"Guessed letters: {guesses}")
print(f"Wrong guesses: {wrong}/{WRONG_LOSE}")

guess = input("Guess a letter: ").strip().upper()

if len(guess) == 1 and guess.isalpha():
if guess in guesses.upper():
print(f"You already guessed '{guess}'. Try again.")
else:
return guess
else:
print("Invalid input. Please guess a single letter.")





def puzzle():
"""
Randomly choose a clue and answer for the puzzle.

Returns:
tuple: A tuple containing the clue and the answer.
"""
puzzles = [
("Sports Team", "TORONTO RAPTORS"),
("80's Movie", "RAIDERS OF THE LOST ARK"),
("Canadian City", "HALIFAX, NOVA SCOTIA")
]
return choice(puzzles)




def play():
# Initialize the game
clue, answer = puzzle()
guesses = ""
wrong = 0
display = hideLetters(answer, guesses)

# Letter guessing loop
while wrong < WRONG_LOSE and "*" in display:
letter = askPlayer(clue, display, guesses, wrong)
guesses += letter
if letter in answer:
display = hideLetters(answer, guesses)
else:
wrong += 1

# End the game
if wrong == WRONG_LOSE:
print("You lose!")
else:
print("You win!")
print(answer)


# Play the game!
play()
5 changes: 5 additions & 0 deletions participants/Sajith.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
name: Sajith Minrutha
Role: Highschool student
github: SajithMinrutha
---