2 Replies - 936 Views - Last Post: 04 December 2012 - 11:38 PM Rate Topic: -----

#1 rainbowstitch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-December 12

In Hangman, how do I get the game to end?!

Posted 04 December 2012 - 08:01 PM

import random
dictionary=['ABRUPT','BLITZ','BUCCANEER','CROISSANT','BEFUDDLE','GIZMO','HAIKU','JOVIAL','NUMB','SQUAWK','THE','MAGIC','ARTIST','TRAVEL','NOWADAYS','ITALIAN','MOUSTACHE','GLOOMY','STARTLED','AMBIGUOUS','GREEN','NUMBER','HANDYMAN','RATCHET','BOILED','ENCRUSTED','AROUND','CHIMPANZEE','PARAKEET','BOOTSTRAPS']
difficulty=str(input("would you like an easy, medium, or hard word?"))
for word in dictionary:
    if difficulty == "easy":
        word=random.choice(dictionary[21:31])
    elif difficulty=="medium":
        word=random.choice(dictionary[11:21])
    elif difficulty=="hard":
        word=random.choice(dictionary[0:11])
wordLength=len(word)
wrongGuesses=''
rightGuesses=''
numGuesses=0
guesses=wordLength*[' _ ']
alreadyGuessed=''
maxWrong=6



def whatLevel(difficulty):
    if difficulty == "easy":
        print(word)
    elif difficulty == "medium":
        print(word)
    elif difficulty == "hard":
         print(word)
        
    else:
        print("Error: Please try again and type in 'easy', 'medium', or 'hard' for your difficulty level:")

def printRules(maxWrong, wordLength):
    wordLength=1
    print("Guess the word in 7 tries!")
    return
def getGuess():
    print
    guess=input("Guess a CAPITAL letter: ")
    guess.strip()
    guess.upper()
    print()
    return guess
  

printRules(maxWrong, wordLength)


def nextGame():
    print('Play Again?')
    return input().lower().startswith('y')
gameOver=False

while True:
    letter=getGuess()
    if len(letter)==1 and letter in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
        if alreadyGuessed.find(letter)!= -1:
            print ("You already picked", letter)
        else:
            alreadyGuessed=alreadyGuessed+letter
            firsttryWord=word.find(letter)
            if firsttryWord==-1:
                wrongGuesses=wrongGuesses+letter
                print("Oops! ",letter,"is not in the word.")
            else:
                print("Yes! ",letter,"is in the word!")
                rightGuesses=rightGuesses+letter
                for instances in range(wordLength):
                    if letter == word[instances]:
                        guesses[instances]=letter
                    allLettersFound=True
                    for instances in range(wordLength):
                        if word not in rightGuesses:
                            allLettersFound=False
                        break
                        if allLettersFound:
                            print("You win!")
    else:
        print("Please guess a single alphabetical letter!")
    print(''.join(guesses))
    print ("Missed Letters: ", wrongGuesses)
    if wrongGuesses==maxWrong:
        print ("You lose!")
        print("The word was",word)

    if letter in word:
        rightGuesses= rightGuesses+letter
        allLettersFound=True
    for i in range(wordLength):
        if word not in rightGuesses:
            allLettersFound=False
            break
        if allLettersFound:
            print("You win!")
            print("The word was", word)

getGuess()
nextGame()



Is This A Good Question/Topic? 0
  • +

Replies To: In Hangman, how do I get the game to end?!

#2 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: In Hangman, how do I get the game to end?!

Posted 04 December 2012 - 09:28 PM

put a break statement in-line with the for statement in the final loop in your code.



Wait a minute... There's a lot of stuff going on in your code that I'm not sure you realize...

The two most important things you're not doing are:
A.) Counting how many wrong guesses have been made


In your code, you ask
if wrongGuesses==maxWrong:
...

You say that wrongGuesses is:
wrongGuesses=wrongGuesses+letter


wrongGuesses is, therefore, a string. maxWrong is an int (6, as it turns out). "6" != 6, "" != 6, no "<stuff>" == 6. 6 == 6, int("6") == 6, but nothing else is. Therefore, wrongGuesses is NEVER equal to maxWrong.



B.)Deciding when the game is over.

You're doing a lot of complicated stuff for something that should be really easy. Given that, it's easy to see why you forgot to use the gameOver variable.

Instead of while True, maybe it should be while not gameOver?

Where do you think you need to set the gameOver variable to True?

Where else does that necessitate checking gameOver to see if it is True or False? What should do if gameOver is True?

This post has been edited by Python_4_President: 04 December 2012 - 09:55 PM

Was This Post Helpful? 0
  • +
  • -

#3 Hqtitan  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 51
  • Joined: 28-August 10

Re: In Hangman, how do I get the game to end?!

Posted 04 December 2012 - 11:38 PM

What is the point of that last for loop? It's going to run as many times as the word is long, unless all of the letters are not found, at which point you force the loop to exit. It seems like it would be better to declare allLettersFound as False from the start? Then you could have an if statement checking if word is in rightGuesses, and run the prints if it is.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1