better python 'hangman' style game
There's a for loop in there that I used, but I'm not sure exactly what it does. I need to research that and see. But it works. I also need to get this to a point where it can use any words, and select them randomly from a list, but for now I got to do more class stuff and move on.
def word_game():
# Explain the game
print("\tWord game start.....Type 'quit' to exit game.")
print("\tYou will be told when you get a correct letter.")
print("\tTo guess the whole word, type in the whole word.")
print("\tGuess my 7 letter word!")
# create a counter to keep track of guesses player makes
count = 0
# create a list of each character in the word so you can tell the player if they got one correct
word = ['c','a','r','r','o','t','s']
# add players used guesses to a list that will display
used_letters = []
# create a bool set to True so the while will loop
play_game = True
# add a clue:
clue = ['X','X','X','X','X','X','X']
# play the game until a condition is met
while play_game:
# get the users new guess each start of loop
print(clue)
print(f"\t Used letters = {used_letters}")
guess = input("\t Your Guess = : ")
# make sure the user can quit. Put this at the top of the if's
if guess == 'quit':
print("............ ok, another time ........")
exit(0)
# check for a correct whole word guess first
# check if the guess is in the list for the word
elif guess in used_letters:
print("!"*63)
print(f"\t\tYou already used the letter {guess}")
elif guess in word:
# change the clue to refelct correct letters:
for i,x in enumerate(word):
if x is guess:
clue[i] = guess
# add to count
count += 1
# check if they won before continuing
if clue == word:
print(f"\t\t {word} ")
print("\t>>>>>> You got my Carrots! <<<<<<")
print(f"\t----------- number of guesses = {count} ---------")
# end game. Maybe go somewhere else now
play_game = False
# if they haven't got the whole word, continue
else:
# append the correct guess to a list the player can see
used_letters.append(guess)
print("?" * 63)
elif guess not in word:
count += 1
# tell them the letter is wrong.
print("\tThis is a wrong letter.")
print(" \t\t~~~~~~~ NOPE ~~~~~~~~")
# add the guess to used_letters
used_letters.append(guess)
else:
# in case something fails
print("That will not work. Try again")
word_game()
Comments
Post a Comment