1

I just started to learn python about a week ago. I tried to create a simple hangman game today. All of my code in this works so far, but there is one thing that I cannot think of how to implement. I want the code to print 'you win' when it the player correctly types 'python', letter by letter. But I cant seem to end it after they get it right. It will end if they type 'python' in one attempt, opposed to letter form. My attempt to do it is on the line with the .join. I can't seem to figure it out though. Any help or advice for a new programmer would be greatly appreciated.

guesses = []
count = 1
ans = 'python'
word = ''


while count < 10:
  guess = raw_input('guess a letter: ')
  guesses.append(guess)
  if ''.join(word) == ans:
    print 'you win'
    break
  elif len(guess) > 1 and ans == guess:
    print ans
    print 'you win'
    break
  else:
    for char in ans:
      if char in guesses:
        word.append(char)
        print char, 
      else:
        print '_', 
    count += 1
else:
  print '\nyou lose'
Jac Frall
  • 403
  • 7
  • 15

1 Answers1

1

First, I want to start off by saying, unless you are dealing with legacy code or some library which you need that only works in 2.7, do not use python 2.7, instead use python 3.x (currently on 3.6). This is because soon 2.7 will be deprecated, and 3.6 + has a lot more features and a lot of QOL improvements to the syntax and language you will appreciate (and has support for functionality that 2.7 just doesn't have now).

With that said, I'll make the translation to 3.6 for you. it barely makes a difference.

guesses = []
count = 1
ans = 'python'
word = ''

while count < 10:
    guess = input('guess a letter: ')
    guesses.append(guess)
    if ''.join(word) == ans:
        print('you win')
        break
    elif len(guess) > 1 and ans == guess:
        print(ans)
        print('you win')
        break
    else:
        for char in ans:
            if char in guesses:
                word.append(char)
                print(char)
            else:
                print('_')
        count += 1
else:
    print('\nyou lose')

The only two changes here are that print now requires parenthesis, so every print 'stuff' is now print('stuff'), and raw_input is now input('input prompt'). Other than that, I'm suprised you were able to get away with word.append(char). You cannot use append() on a python str in either 2.7 or 3.x. I think you were trying to use it as an array, as that is the only reason you would use ''.join(word). To fix this I would do word = [] instead of word = ''. now your ''.join(word) should work properly.

I would advise you to take the next step and try to implement the following things to your program: If the user doesn't enter a single character, make it so that the characters are not added to the guesses list. Try making this a main.py file if you haven't already. Make parts of the program into functions. Add a new game command. Add an actual hangman in chars print out every time. Add file io to read guess words (ie instead of just python, you could add a lot of words inside of a file to chose).

Community
  • 1
  • 1
Krupip
  • 4,404
  • 2
  • 32
  • 54