2 Replies - 571 Views - Last Post: 05 December 2012 - 12:32 PM Rate Topic: -----

#1 rainbowstitch  Icon User is offline

  • New D.I.C Head

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

Can't Figure Out How To Display "Win" in Hangman Program:

Posted 05 December 2012 - 10:47 AM

Hey there. I posted an earlier version of this code earlier and I still can't seem to figure out how to exit the loop when all of the blanks have been filled in. This is my first larger program and so I'm having a lot of difficulties with it. If somebody with a bit more experience could scan through it and give me a few suggestions, I'd really appreciate it! A lot!

So, here it is:

 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
gamePoints=0

gameOver=numGuesses>=maxWrong



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():
    playAgainResponse=input('Play Again?')
    if input==("Yes"):
        difficulty
        wrongGuesses=''
        rightGuesses=''
        gamePoints=gamePoints+rightGuesses
    


while not gameOver:
    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
          
            
                
                    
    else:
        print("Please guess a single alphabetical letter!")
    print(''.join(guesses))
    print ("Missed Letters: ", wrongGuesses)
    if len(wrongGuesses)==maxWrong:
        print ("You lose!")
        print("The word was "+word)
        if gamePoints == int:
            print("You have "+gamePoints+" points!")
        else:
            print("You have 0 points.")
        gameOver
        nextGame()


for i in range(wordLength):
        if word[i] not in rightGuesses:
            allLettersFound=False
            break
        if allLettersFound:
            gamePoints=gamePoints+10
            print("You win!")
            print("The word was", word)
            print("You have",gamePoints,"points!")
            gameOver
            nextGame()
            break

            

getGuess()
nextGame()



Is This A Good Question/Topic? 0
  • +

Replies To: Can't Figure Out How To Display "Win" in Hangman Program:

#2 woooee  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 78
  • Joined: 21-November 12

Re: Can't Figure Out How To Display "Win" in Hangman Program:

Posted 05 December 2012 - 12:19 PM

You should first test your code before posting to get meaningful responses. I get an error on this line
guesses[instances]=letter


It first depends an whether you want to replace all locations for one letter if it is found or not. Since you print "you have already guessed that letter" I will assume that you replace all letters found in the original word. You then compare the guessed word so far to the original word. It is easier to work with lists since you can replace a letter at a given location. Obviously the following is not complete.
def get_input_and_check_for_winner():
    ## simulate some guesses
    for ltr in ["A", "N", "E", "S", "G", "U", "X"]:
        if ltr in word_to_guess:
            if ltr in already_guessed:
                print "You already guessed that letter"
            else:
                ## if letter found, replace the "*" in that location
                ## with the correct letter
                for ctr, each_letter in enumerate(word_to_guess):
                    if ltr == each_letter:
                        guessed_list[ctr]=ltr     ## replace with guessed

            print ltr, guessed_list

            ## test for win
            if word_to_guess == guessed_list:
                print "***** Winner *****", "".join(guessed_list)
                return True

    return False

word_to_guess=list("GUESS")
already_guessed=[]
guessed_list=["*" for x in word_to_guess]
print guessed_list

print get_input_and_check_for_winner() 

Was This Post Helpful? 0
  • +
  • -

#3 woooee  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 78
  • Joined: 21-November 12

Re: Can't Figure Out How To Display "Win" in Hangman Program:

Posted 05 December 2012 - 12:32 PM

And your input function should be more along the lines of
def getGuess(already_guessed):
    while True
        print
        guess=input("Guess a CAPITAL letter: ")
        guess.strip()
        guess.upper()

        if guess in already_guessed:
            print "You already guessed that letter"
        else:        
            return guess 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1