Advertisement
makispaiktis

Hangman

Mar 7th, 2021 (edited)
1,083
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.10 KB | None | 0 0
  1. from timeit import default_timer as timer
  2. from random import randrange
  3.  
  4.  
  5. # Function 1
  6. def writeProgress(letters, foundLetters):
  7.     result = ""
  8.     for i in range(len(foundLetters)):
  9.         if foundLetters[i] == 1:
  10.             result += letters[i] + " "
  11.         else:
  12.             result += "- "
  13.     return result
  14.  
  15.  
  16. # 1. Python program to read file word by word
  17. # Opening the text file
  18. words = list()
  19. with open('words.txt', 'r') as file:
  20.     for line in file:
  21.         for word in line.split():
  22.             words.append(word)
  23.  
  24. # print(words)
  25. # print("There are " + str(len(words)) + " words")
  26. print()
  27. print("*********************************************************************************")
  28. print("*********************************************************************************")
  29. print("Welcome to the 'Hangman Game'. You will have to make 2 choices to start the game.")
  30. # 2. Take the user's choices as data
  31. attempts = int(input("1. How many attempts do you want to have? "))
  32. while attempts <= 0 or attempts > 25 or attempts != int(attempts):
  33.     incorrect = int(input("1. How many incorrect answers do you want to have? "))
  34. NLimit = int(input("2. What minimum word length do you want? "))
  35. while NLimit < 4 or NLimit > 16:
  36.     NLimit = int(input("2. What minimum word length do you want? "))
  37.  
  38. # 3. Randomize the word according to given criteria
  39. word = words[randrange(0, len(words))]
  40. # print(word)
  41. while len(word) < NLimit:
  42.     word = words[randrange(0, len(words))]
  43.     # print(word)
  44.  
  45.  
  46. # 4. Let the game begin
  47. print()
  48. # print(word)
  49. letters = [word[i].lower() for i in range(len(word))]
  50. # I will make a list in which I match each letter with 0 or 1
  51. # The element will be 0 if this letter is not revealed yet
  52. foundLetters = [0 for _ in range(len(letters))]
  53. print("Your word is ready and has " + str(len(word)) + " letters.")
  54. print(writeProgress(letters, foundLetters))
  55. attemptsRemaining = attempts
  56. print("Attempts reamining: " + str(attemptsRemaining))
  57.  
  58. # 5. While loop
  59. print()
  60. print()
  61. counter = 0
  62. lettersGuessed = list()
  63. wordFound = False
  64.  
  65. while attemptsRemaining > 0 and wordFound == False:
  66.     counter += 1
  67.     print("~~~~~~~~~~~~~~~~ Round " + str(counter) + " ~~~~~~~~~~~~~~~~")
  68.     print("    Used letters = " + str(lettersGuessed))
  69.     letterGuessed = input("Guess a letter: ")
  70.     # a) First, I will check if the user has guessed this letter again. I will give him 1 more chance.
  71.     for i in range(len(lettersGuessed)):
  72.         if letterGuessed == lettersGuessed[i]:
  73.             print("You guessed this letter (" + letterGuessed + ") again. I you do it again, I will count it as 'fault'.")
  74.             letterGuessed = input("Now, try to guess another letter: ")
  75.             break
  76.     lettersGuessed.append(letterGuessed)
  77.     # b) Now, we have a valid try/guess for the next letter and we have to check if this letter
  78.     # exists in ouy random word
  79.     exists = False
  80.     for i in range(len(letters)):
  81.         if letterGuessed == letters[i]:
  82.             foundLetters[i] = 1
  83.             exists = True
  84.             # I won't break here, because maybe 1 letter exists more than once
  85.     # c) Last step is to reduce the remaining attempts in case of a wrong guess
  86.     if exists == False:
  87.         attemptsRemaining -= 1
  88.         print(letterGuessed + " is NOT in our word.")
  89.     else:
  90.         print(letterGuessed + " is IN our word.")
  91.     # d) Check if I have found the word
  92.     found = True
  93.     for i in range(len(foundLetters)):
  94.         if foundLetters[i] == 0:
  95.             found = False
  96.             break
  97.     wordFound = found
  98.     # e) Show the progress of the word's guesses
  99.     print(writeProgress(letters, foundLetters))
  100.     print("Attempts remaining: " + str(attemptsRemaining))
  101.     print()
  102.  
  103. if attemptsRemaining == 0:
  104.     print()
  105.     print("You failed to guess the word correctly, but your try was a good one.")
  106.     print("The word was ----> " + str(word))
  107. else:
  108.     print()
  109.     print("Congratulations!!!!")
  110.     print("You made " + str(attempts - attemptsRemaining) + "/" + str(attempts) + " faults and found the correct word")
  111.     print("The word was ----> " + str(word))
  112. print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement