Tyler Pelton
Tyler Pelton

Reputation: 19

Trying to make Hangman in Python, only been using python for around a day

This is my first day of learning python, with a decent amount of java background. This simple hangman program is working as intended, except for one frustrating problem. If your guess isn't the first letter in the word, you will automatically get a "strike", even if they letter was correct elsewhere in the word. I have pinpointed where and why the problem occurs, but I cannot seem to find a solution. If anyone has any tips they would be greatly appreciated.

 #hangman
# 0
#\|/
# |
#/ \
print('Welcome to hangman 2000!')
word=input('Enter your word: ')
lengthOfWord=len(word)
guessed=list('_'*lengthOfWord)
hangman=''
# o \n\\|/\n | \n/ \\
while True:
    wordActual=list(word)
    for i in range(0,lengthOfWord):
        print(guessed[i],end=' ')
    print('\n'+hangman)
    guess=input('Guess a letter: ')
    for i in range(0,lengthOfWord):
        if(wordActual[i]==guess):
            guessed[i]=guess


    for i in range(0,lengthOfWord):

        if((wordActual[i]!=guess)==False):
            print("test point")
            break

    #THE PROBLEM IS RIGHT HERE^, IF THE FIRST LETTER ISN'T YOUR FIRST GUESS IT WILL ALWAYS GO INTO THE HANGMAN MAKER\/

        if(hangman==' o \n\\|/\n | \n/ \\'):
            print('GAME OVER ')
            input('Press Any Key to accept your failure...')

            exit()
        elif(hangman==''):
            hangman+=' o \n'
            break
        elif(hangman==' o \n'):
            hangman+='\\'
            break
        elif(hangman==' o \n\\'):
            hangman+='|'
            break
        elif(hangman==' o \n\\|'):
            hangman+='/\n '
            break
        elif(hangman==' o \n\\|/\n '):
            hangman+='| \n'
            break
        elif(hangman==' o \n\\|/\n | \n'):
            hangman+='/ '
            break
        elif(hangman==' o \n\\|/\n | \n/ '):
            hangman+='\\'
            break


    if(wordActual==guessed):
        print('Congradulations!')
        input('Press Any Key to exit...')
        break

Upvotes: 0

Views: 171

Answers (3)

Kevin
Kevin

Reputation: 23

### try to use a set ###
mport random
import math
something = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
correct_word = something[random.randint(0, len(something) - 1)]
correct_s = set()
left = 100
for s in correct_word:
  correct_s.add(s)
gusses = len(correct_word) * 2#math.ceil(len(correct_word)/2)
space = "_ "
win = False
guss = set()
blob = ""
for i in range(len(correct_word)):
  blob += "_ "
print(blob + "GUESS LEFT: " + str(gusses))

while not win:

  gus = input("ENTER A LETTER: ")
  left = 0
  if gus in correct_s:
    #correct_s.remove(gus)
    now = ""
    for l in correct_word:
      guss.add(gus)
      if l in guss:
        now += l + " "
      else:
        now += "_ "
        left += 1
    print("GREAT!")
  else:
    gusses -= 1
    now = ""
    for l in correct_word:
      if l in guss:
        now += l + " "
      else:
        now += "_ "
        left += 1
    print("OOPS!")
  print(now + "GUESS LEFT: " + str(gusses))
  if left == 0:
    print("CONGRATULATIONS, YOU WIN!")
    win = True
  if gusses <= 0:
    print("SORRY, YOU ARE OUT OF GUSSES!")
    print("THE RIGHT WORD IS: " + correct_word)
    break

Upvotes: 0

aghast
aghast

Reputation: 15310

I'd suggest making a bunch of changes. Let's take them in order of importance:

  1. It's not totally clear how you want to handle repeated letters. If the word is 'book' and I guess 'o', do I get one letter or two? Can I guess 'o' again?

    This will have an effect on how you store things. If you're doing "Wheel of Fortune"-type rules, where all the matching letters are revealed, you can just store the word as a string. But if you are going to require a separate guess for each letter, it will be better to have the actual word be modifiable, so you can mark them off. That means a list. (Array, in java)

  2. You're doing a bunch of repeated work. There are python idioms and operators for things like concatenating a bunch of letters:

    print(''.join(list-of-letters))
    

    Also, iterating over a string or list checking to see if an item is contained:

    if letter in list-of-letters: 
    

    Also, iterating over a string or list to find the index of an element:

    i = stringVariable.index(item)
    

    There's also str.find() for strings, which doesn't raise exceptions.

  3. I'd recommend keeping score with an integer. You can create an array of possible hangman values and print the right one.

  4. There's this pesky bug:

        for i in range(0,lengthOfWord):
    
            if((wordActual[i]!=guess)==False):
                break
    
            if(hangman==' o \n\\|/\n | \n/ \\'):
            ... etc.
    

    Take a hard look. The problem is indentation - you've got the second if statement inside the block of the for. It should be afterwards, with some kind of "found/not-found" variable falling out. Otherwise, it checks the first letter and if not guessed goes right into the "grow the hangman" code.

Upvotes: 0

Joran Beasley
Joran Beasley

Reputation: 113988

you can check if a letter is in a word much easier with if letter in word ... I think you can refactor it

just apply the following fixes ...

get rid of for i in range(0,lengthOfWord):

and replace

 if((wordActual[i]!=guess)==False):
        print("test point")
        break

with

 if guess in wordActual:
     print("test point")
     break

the rest is fine

Upvotes: 1

Related Questions