1 Replies - 700 Views - Last Post: 03 September 2015 - 10:41 AM Rate Topic: -----

#1 syked3   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 25-August 15

Something is going wrong in this program, help!

Posted 03 September 2015 - 06:35 AM

Hey guys,
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:
#player gets 5 chances

#computer only respond with yes/no

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 correct==word:
    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.
Any help would be appreciated.
syked3

Is This A Good Question/Topic? 0
  • +

Replies To: Something is going wrong in this program, help!

#2 DK3250   User is online

  • Pythonian
  • member icon

Reputation: 571
  • View blog
  • Posts: 1,836
  • Joined: 27-December 13

Re: Something is going wrong in this program, help!

Posted 03 September 2015 - 10:41 AM

Hi, without giving you detailed code:
Try to compare line 16 and line 34 - you probably need to correct something.
If you want to run a fixed number of guesses (5 guesses in your case), you should use a for-loop, not a while-loop.
Your line 23 will loop as many time as there are letters in word - this is probably not what you want...
You use same variable ('i') as loop-counter and input (line 23, 24), not a good idea.

I hope this will push you forward.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1