Skip to content

Testing code #212

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Python/3sum-closest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ def threeSumClosest(self, nums, target):
min_diff = abs(total-target)
result = total
return result

˝
59 changes: 59 additions & 0 deletions Python/word_guess.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import random

class WordGuess:
# choices = all possible target words for the guessing game
choices = ["able", "belt", "bolt", "cast", "cash", "knot", "note", "near", "over", "salt", "wind"]
choices_set = set()
target = ""
wrong_msg = "Wrong guess! Try again:"
correct_msg = "You got it! Amazing!"
game_over = "You're out of turns, game over!"
new_game_msg = "Welcome to Word Guess! You have 5 turns to guess the word. Please enter your first guess:"
not_choice_msg = "The word you guesssed is not a possible answer."
max_turns = 5
curr_turn = 0
is_over = False
chars_in_tgt = [0] * 26

def __init__(self):
print(self.new_game_msg)
self.choices_set = set(self.choices)

def choose_target(self):
self.target = random.choice(self.choices)
for c in self.target:
self.chars_in_tgt[ord(c) - ord('a')] += 1

def guess(self, guess: str):
if guess not in self.choices_set or guess == "":
print(self.not_choice_msg)
print(f"Guesses left {self.max_turns - self.curr_turn}")
return
is_correct = guess == self.target
if is_correct:
print(self.correct_msg)
self.is_over = True
return
else:
print(self.wrong_msg)
for i in range(len(guess)):
c = guess[i]
to_print = "-"
if self.chars_in_tgt[ord(c) - ord('a')] and c != self.target[i]:
to_print = "0"
elif c == self.target[i]:
to_print = "1"
print(to_print, end="")
print("")
self.curr_turn += 1
print(f"Guesses left {self.max_turns - self.curr_turn}")
if self.curr_turn == self.max_turns:
print(self.game_over)
self.is_over = True

wg = WordGuess()
wg.choose_target()
print(wg.target)
while not wg.is_over:
a=input()
wg.guess(a)