On Sat, Jul 2, 2016 at 8:29 AM Alan Gauld via Tutor <tutor@python.org> wrote:
> There are arguably easier ways of doing this > I think you'll find that for-loops are preferable to while-loops. Here's an alternative implementation. https://gist.github.com/selik/d8e0a7622ceff0fe8984a7d19d44bfca import random import string drawings = ( r""" ------| | | | | -- """, r""" ------| | 0 | | | -- """, r""" ------| | 0 | -+- | | -- """, r""" ------| | 0 | /-+- | | -- """, r""" ------| | 0 | /-+-\ | | -- """, r""" ------| | 0 | /-+-\ | | | -- """, r""" ------| | 0 | /-+-\ | | | | -- """, r""" ------| | 0 | /-+-\ | | | | | -- """ ) print('Welcome to Hangman.') print('Good luck!') words = 'python ruby php java unix linux perl'.split() target = list(random.choice(words)) known = ['_'] * len(target) used = [] for drawing in drawings: print('-'.join(c if c not in used else ' ' for c in string.ascii_lowercase)) print(drawing, '\n\t', ' '.join(known)) guess = input('\nEnter your guess: ').lower() while guess in used: print("You've already guessed that letter") guess = input('Please enter a new guess: ').lower() used.append(guess) if guess in target: print('Yes, {!r} is in the word!'.format(guess)) for i, letter in enumerate(target): if guess == letter: known[i] = letter else: print('Sorry, {!r} is not in the word.'.format(guess)) if known == target: print('\nYou guessed the word correctly!') break else: print('\nYou have been hanged!') print('The word was {!r}'.format(''.join(target))) _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor