0

I'm new to python and have been working through a book. At the end of the chapter there was a challenge 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. The player gets five chances to ask if the letter is in the word. The computer responds with only a yes/no. The player then has to guess the word. Here is my attempt at this:

print ("\t\t\tWelcome to the guess my word challenge")
print ("\nThe computer will pick and random word and you have to guess it in five tries")

import random

#create sequence of words
WORDS = ("computer","laptop","mouse","keyboard")

#pick random word
word=random.choice(WORDS)

correct=word
tries=0

print ("This word has ",len(word), "letters")

i=""
while word != "" and tries<5:
    for i in word:
        i=input("Guess a letter: ")
        if i not in word:
            print ("No")
        else:
            print ("\nYes")
    tries+=1
if tries==5:
        print("You've run out of tries")

final=input("\nGuess my word: ")
if word==correct:
    print ("Well done that was my word!")
else:
    print ("Better luck next time")

input ("\n\nPress the enter key to exit")

Now the problem I'm having is I can't get the tries bit to work, eventually the program will say you've run out of tries but I want it to say that after 5 tries. Also, no matter what word I put in at "guess my word", it always displays its correct even if it isn't.

1
  • You never use final after you ask for it - did you intend to compare final==correct? Commented Sep 3, 2015 at 14:29

3 Answers 3

1

There are two problems with your code.

  1. You ask the user for a letter for each letter in the word, and only then increment the tries counter, then ask again for each letter in the word. Remove the for i in word: loop.

  2. You compare the word to correct, which is just the word itself. Instead, you have to compare it to the user's final guess, i.e. if word==final:

With those two fixes, it should work.

Sign up to request clarification or add additional context in comments.

Comments

0

You want to count down the number of times the player guesses. The for loop

for i in word:

will cycle one time for each letter in the word, setting i equal to that letter, but you then reassign i to user input. So if you remove the for loop, it should run as intended.

i=""
while word != "" and tries<5:
    i=input("Guess a letter: ")
    if i not in word:
        print ("No")
    else:
        print ("\nYes")
    tries+=1
if tries==5:
    print("You've run out of tries")  

[EDIT] Also you're not comparing the correct word to the user input, so you want to say

if correct == final :

Comments

0
import random

word = random.choice(['computer','laptop','mouse','keyboard'])

def guess(tries = 0):
    letter = input('Guess a letter: ')
    if letter in word:
        print('\nYes')
    else:
        print('No')
    tries += 1
    if tries == 5:
        return
    guess(tries)

print('\t\t\tWelcome to the guess my word challenge')
print('\nThe computer will pick and random word and you have to guess it in five tries')
print('This word has {0} letters'.format(len(word)))

guess()

final = input('\nGuess my word: ')

if final == word:
    print('Well done that was my word!')
else:
    print('Better luck next time')

input('\n\nPress the enter key to exit')

1 Comment

Instead of just providing correct code, it would be more beneficial to explain why the code is correct / his code was wrong. That way people can learn rather than just copy paste

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.