Reputation: 25
I have to create a game where the computer picks a random word and the player has to guess that word. The computer tells the player how many letters are in the word. Then the player gets five chances to ask if a letter is in the word. The computer can only respond with "yes"
or "no"
. Then, the player must guess the word.
I only have:
import random
WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone", "truck" , "doom" , "mayonase" ,"flying" ,"magic" ,"mine" ,"bugle")
word = random.choice(WORDS)
print(len(word))
correct = word
guess = input("\nYour guess: ")
if guess != correct and guess != "" :
print("No.")
if guess == correct:
print("Yes!\n")
I have no idea how to do this problem.
Upvotes: 2
Views: 356
Reputation: 76907
You are looking for something like below
import random
WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone", "truck" , "doom" , "mayonase" ,"flying" ,"magic" ,"mine" ,"bugle")
word = random.choice(WORDS)
correct_answer = word
max_guesses = 5
print("Word length:", len(word))
print("Attempts Available:", max_guesses)
for guesses in range(max_guesses):
guess = input("\nEnter your guess, or a letter: ")
if guess == correct_answer:
print("Yay! '%s' is the correct answer.\n" % guess)
break
elif guess != "":
if guess[0] in correct_answer:
print("Yes, '%s' appears in the answer" % guess[0])
else:
print("No, '%s' does not appear in the answer" % guess[0])
else:
print("\nYou ran out of maximumum tries!\n")
Upvotes: 0
Reputation: 384
I assume you want to let the user ask the computer if a letter is in the word, 5 times. If so, here is the code:
for i in range(5): #Let the player ask 5 times
letter = input("What letter do you want to ask about? ")[0]
#take only the 1st letter if they try to cheat
if letter in correct:
print("yes, letter is in word\n")
else:
print("no, letter is not in word")
The key is the in
operator within the for
loop.
Upvotes: 1