I'm a 16 year old who has picked up coding recently using the codecademy course on Python. I completed the last of the lessons yesterday and decided to start on a simple Hangman game. As this is my first free-form project, it took me around 7h to finish it (a number i'm not proud of, i admit).
Here is the main body:
import string import Hidden_word try_again = 'y' while try_again == 'y': word = Hidden_word.HiddenWord() hidden_word = word.hide_word() shown_word = word.show_word() chances_left = int(len(shown_word) / 2 + 1) misses = 0 used_letters =  print("Here is the hidden word:", ' '.join(hidden_word)) print("You have %d tries" % chances_left) while chances_left > 0: letter = input("Choose a letter: ").lower() while True: if not letter in string.ascii_lowercase or letter == '': letter = input("Please provide a valid answer: ").lower() elif letter in used_letters: letter = input("You've already used this letter, try another one: ").lower() else: used_letters.append(letter) break if letter in shown_word: print("Bingo!") for x in range(len(shown_word)): if shown_word[x] == letter: hidden_word[x] = letter print(' '.join(hidden_word)) else: chances_left -= 1 if chances_left > 0: print ("Whoops, there is no '%s' in this word, try again! (%d chance(s) left)" % (letter, chances_left)) print(' '.join(hidden_word)) misses += 1 if ''.join(hidden_word) == shown_word: if misses == 0: print("Congratulations! You did it without missing a single time!") else: print("Congratulations! You did it! But you've missed %d time(s)" % misses) break elif chances_left == 0: print("There's no '%s' in this word. You've got no tries left" % letter) print("The word was:", shown_word) try_again = input("Would you like to try again ? (y/n)").lower() while not try_again.lower() in ('y', 'n'): try_again = input("Please provide a valid answer: ") print("Good bye!")
and here is the hidden_word class:
import linecache import random def number_lines (): with open("Words.txt", 'r') as file: for i, l in enumerate(file): pass return i + 1 class HiddenWord(object): hidden_word =  def __init__(self): self.shown_word = linecache.getline("Words.txt", random.randint(1, number_lines()))[:-1] def show_word (self): return self.shown_word def hide_word(self): self.hidden_word =  for x in self.shown_word: if x == ' ': self.hidden_word.append(' ') else: self.hidden_word.append('*') return self.hidden_word
I made sure to check all the possible cases, and it works perfectly without a hitch.