Advertisement
makispaiktis

Hangman

Feb 20th, 2021 (edited)
2,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. from timeit import default_timer as timer
  2. from random import randrange
  3.  
  4. # 1. Python program to read file word by word
  5. # Opening the text file
  6. words = list()
  7. with open('words.txt', 'r') as file:
  8.     for line in file:
  9.         for word in line.split():
  10.             words.append(word)
  11.  
  12. # print(words)
  13. # print("There are " + str(len(words)) + " words")
  14. print()
  15. print("*********************************************************************************")
  16. print("*********************************************************************************")
  17. print("Welcome to the 'Hangman Game'. You will have to make 2 choices to start the game.")
  18. # 2. Take the user's choices as data
  19. incorrect = int(input("1. How many incorrect answers do you want to have? "))
  20. while incorrect <= 0 or incorrect > 25 or incorrect != int(incorrect):
  21.     incorrect = int(input("1. How many incorrect answers do you want to have? "))
  22. NLimit = int(input("2. What minimum word length do you want? "))
  23. while NLimit < 4 or NLimit > 16:
  24.     NLimit = int(input("2. What minimum word length do you want? "))
  25.  
  26. # 3. Randomize the word according to given criteria
  27. word = words[randrange(0, len(words))]
  28. # print(word)
  29. while len(word) < NLimit:
  30.     word = words[randrange(0, len(words))]
  31.     # print(word)
  32.  
  33.  
  34. # 4. Let the game begin
  35. print()
  36. print(word)
  37. letters = [word[i] for i in range(len(word))]
  38. # I will make a list in which I match each letter with 0 or 1
  39. # The element will be 0 if this letter is not revealed yet
  40. foundLetters = [0 for _ in range(len(letters))]
  41. print("*********************************************************************************")
  42. attemptsRemaining = incorrect
  43. while attemptsRemaining > 0:
  44.     print(attemptsRemaining)
  45. print("*********************************************************************************")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement