Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. import random, sys
  2. from typing import List
  3. def import_words():
  4. # define empty list
  5. global WORD_LIST
  6. WORD_LIST = []
  7.  
  8. # open file and read the content in a list
  9. with open('listaprojekt.txt', 'r') as filehandle:
  10. filecontents = filehandle.readlines()
  11.  
  12. for line in filecontents:
  13. # remove linebreak which is the last character of the string
  14. current_place = line[:-1]
  15.  
  16. # add item to the list
  17. WORD_LIST.append(current_place)
  18.  
  19.  
  20. def set_values() -> None:
  21. global GUESS_WORD
  22. GUESS_WORD = []
  23. global SECRET_WORD
  24. SECRET_WORD = random.choice(WORD_LIST) # lets randomize single word from the list
  25. global LENGTH_WORD
  26. LENGTH_WORD = len(SECRET_WORD)
  27. global ALPHABET
  28. ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  29. global letter_storage
  30. letter_storage = []
  31.  
  32. def print_word_to_guess(letters: List) -> None:
  33. """Utility function to print the current word to guess"""
  34. print("Word to guess: {0}".format(" ".join(letters)))
  35.  
  36.  
  37. def print_guesses_taken(current: int, total: int) -> None:
  38. """Prints how many chances the player has used"""
  39. print("You are on guess {0}/{1}.".format(current, total))
  40.  
  41.  
  42. # Game functions
  43.  
  44. def beginning() -> None:
  45. """Starts the game"""
  46. print("Hello!\n")
  47. while True:
  48. name = input("Please enter Your name\n").strip()
  49. if name == '':
  50. print("You can't do that! No blank lines")
  51. else:
  52. Print = sys.stdout.write
  53. print("Welcome,", (name),end='\n')
  54. break
  55.  
  56.  
  57. def ask_user_to_play() -> None:
  58. """Ask user if they want to play"""
  59. print("Well, that's perfect moment to play some Hangman!\n")
  60. while True:
  61. gameChoice = input("Would You?\n").upper()
  62. if gameChoice == "YES" or gameChoice == "Y":
  63. break
  64. elif gameChoice == "NO" or gameChoice == "N":
  65. sys.exit("That's a shame! Have a nice day")
  66. else:
  67. print("Please Answer only Yes or No")
  68. continue
  69.  
  70.  
  71. def prepare_secret_word() -> None:
  72. """Prepare secret word and inform user of it"""
  73. for character in SECRET_WORD: # printing blanks for each letter in secret word
  74. GUESS_WORD.append("_")
  75. print("Ok, so the word You need to guess has", LENGTH_WORD, "characters")
  76. print("Be aware that You can enter only 1 letter from a-z\n\n")
  77. print_word_to_guess(GUESS_WORD)
  78.  
  79.  
  80. def guessing() -> None:
  81. """
  82. Main game loop to have user guess letters
  83. and inform them of the results
  84. """
  85. guess_taken = 1
  86. MAX_GUESS = 10
  87. print_guesses_taken(guess_taken, MAX_GUESS)
  88.  
  89. while guess_taken < MAX_GUESS:
  90. guess = input("Pick a letter\n").upper()
  91. if not guess in ALPHABET: #checking input
  92. print("Enter a letter from a-z ALPHABET")
  93. elif guess in letter_storage: #checking if letter has been already used
  94. print("You have already guessed that letter!")
  95. else:
  96. letter_storage.append(guess)
  97. if guess in SECRET_WORD:
  98. print("You guessed correctly!")
  99. for i in range(0, LENGTH_WORD):
  100. if SECRET_WORD[i] == guess:
  101. GUESS_WORD[i] = guess
  102. print_word_to_guess(GUESS_WORD)
  103. print_guesses_taken(guess_taken, MAX_GUESS)
  104. if not '_' in GUESS_WORD:
  105. print("You won!")
  106. print("Game Over!")
  107. break
  108. else:
  109. print("The letter is not in the word. Try Again!")
  110. guess_taken += 1
  111. print_guesses_taken(guess_taken, MAX_GUESS)
  112. if guess_taken == 10:
  113. print(" Sorry Mate, You lost :<! The secret word was {0}".format(SECRET_WORD))
  114. def main():
  115. import_words()
  116. beginning()
  117. ask_user_to_play()
  118. set_values()
  119. prepare_secret_word()
  120. guessing()
  121. while True:
  122. answer = input("Would you like to play again?")
  123. if answer == 'yes':
  124. set_values()
  125. prepare_secret_word()
  126. guessing()
  127. elif answer == 'no':
  128. break
  129. else:
  130. print("dont understand")
  131.  
  132. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement