For the Absolute Beginner 3rd Edition book, I am having trouble with another
question, which says "create a game where the computer picks a random word and
the player must guess that word. The computer tells the player how many letters
are in the word. Then the player gets 5 chances to ask if a letter is in the
word. The computer can only respond with 'yes' or 'no'. The player must then
guess the word."
In the "Loop of Game" section of my code for some reason it gives me five more
chances than I wanted it to. When I put two as the chance limit, it allowed
seven. Also, the program will always say "yes" to any letter I enter, even if
it's wrong.
#Word Guess
import random
Aquino
# create a sequence of words to choose from
WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone")
# pick one word randomly from the sequence
word = random.choice(WORDS)
# create a variable to use later to see if the guess is correct
correct = word
# Getting the letter count and selection
lcount = len(correct)
LETTERS = None
if correct == "python":
LETTERS = "python"
elif correct == "jumble":
LETTERS = "jumble"
elif correct == "easy":
LETTERS = "easy"
elif correct == "difficult":
LETTERS = "difficult"
elif correct == "answer":
LETTERS = "answer"
elif correct == "xylophone":
LETTERS = "xylophone"
#Start Game
print("\n\n This word has", lcount,"letters in it.")
#Loop of Game
chances = 0
while chances < 5:
guess = input("Does the word have a: ")
for letter in correct:
if letter.lower() in LETTERS:
print("\nYes")
chances += 1
guess = input("\nDoes the word have a: ")
else:
print("\nNo")
chances += 1
guess = input("\nDoes the word have a: ")
print("\nYour chances are up!")
theguess = input("What is your guess?: ")
if theguess == correct:
print("\n\nYou did it!")
input("\n\nPress the Enter key to exit.")
else:
print("\n\nSorry the word is:", correct)
print(("Try again!"))
input("\n\nPress the Enter key to exit.")
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor