Advertisement
Guest User

Untitled

a guest
May 27th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. # Written for Python 3.6
  2. # Playable in the Python 3.6.1 Shell
  3. # To play put the files:
  4. # "google-10000-english-usa-no-swears-long.txt"
  5. # "google-10000-english-usa-no-swears-medium.txt"
  6. # "google-10000-english-usa-no-swears-short.txt"
  7. # in your Python36 folder
  8. # To start use: exec(open("./Hangman.py").read())
  9.  
  10. # Start of the progam
  11. print ('Welcome to Hangman!')
  12. start = input('Press enter to continue...\n')
  13. import random
  14. alphabet = list('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') # to be used as a check that a letter is entered
  15. allwords = []
  16. if start != '':
  17. print('That is not the enter key..')
  18. start = input("Press enter to continue, any other key will exit the program.\n")
  19. if start != '':
  20. print ('Exiting program')
  21. quit()
  22.  
  23. #DIFFICULTY
  24. print ('On what difficulty do you want to play?\n')
  25. difficulty = input('1.Easy\n2.Medium\n3.Hard\n\n')
  26.  
  27. # diffvalues = ['1','2','3','easy','Easy','EASY','medium','Medium','MEDIUM','hard','Hard','HARD']
  28. # while difficulty not in diffvalues:
  29. ## Wat als ik nou EaSy wil doen? Met de lower functie kan je een string in lowercase veranderen!
  30. while difficulty.lower() not in ['1','2','3','easy','medium','hard']:
  31. print ('Sorry, that was not one of the choices')
  32. print ('On what difficulty do you want to play?\n')
  33. difficulty = input('1.Easy\n2.Medium\n3.Hard\n\n')
  34.  
  35. # if difficulty == '1' or difficulty == 'Easy' or difficulty == 'easy' or difficulty == 'EASY':
  36. if difficulty.lower() in ['1', 'easy']:
  37. with open('google-10000-english-usa-no-swears-short.txt') as inputfile:
  38. for line in inputfile:
  39. allwords.extend(line.strip().split(','))
  40. # elif difficulty == '2' or difficulty == 'Medium' or difficulty == 'medium' or difficulty == 'MEDIUM':
  41. elif difficulty.lower() in ['2', 'medium']:
  42. with open('google-10000-english-usa-no-swears-medium.txt') as inputfile:
  43. for line in inputfile:
  44. allwords.extend(line.strip().split(','))
  45. # elif difficulty == '3' or difficulty == 'Hard' or difficulty == 'hard' or difficulty == 'HARD':
  46. elif difficulty.lower() in ['3', 'hard']:
  47. with open('google-10000-english-usa-no-swears-long.txt') as inputfile:
  48. for line in inputfile:
  49. allwords.extend(line.strip().split(','))
  50.  
  51. # In bovenstaande stuk heb je wel drie keer precies hetzelfde gedaan om een bestand uit te lezen. Dit zou je ook kunnen veranden in een functie bijv:
  52. def readFile(filename):
  53. wordsInFile = []
  54. with open(filename) as inputfile:
  55. for line in inputfile:
  56. wordsInFile.extemd(line.strip().split(','))
  57.  
  58. return wordsInFile
  59.  
  60. print ("All right then, let's start.\n")
  61.  
  62. # GAME VARIABLES
  63. words = ['apple', 'banana', 'coconut'] # to test it in a webinterpreter, change word = random.choice(allwords) into word = random.choice(words)
  64. word = random.choice(allwords) # random selection from list
  65. #print (word) # for testing, this is the word chosen, needs to be removed for the actual game
  66. length = len(word) # To determine the number of underscores in uslist
  67. uslist = ['_'] * length # A list with as much underscores as letters in the word
  68. hangword = list(word) # The randomly selected word as a list
  69. guessesleft = 8 # Amount of guesses
  70. sentence1 = ('You have %d guesses!\n') % guessesleft # Standard sentence used later on to count
  71. print ('')
  72. print('The word looks like this:\n')
  73. print (' '.join(uslist))
  74. print ('')
  75. print (sentence1)
  76. print ('')
  77.  
  78. # Start of the game
  79. playerguess = input('Guess a letter..\n')
  80. playerguesslow = playerguess.lower()
  81. while playerguesslow not in alphabet or len(playerguess) > 1 or playerguess == '': # Prevents the player from entering non-letters, multiple letters or enter
  82. playerguess = input ('That is not a letter, please try again.\n')
  83. playerguesslow = playerguess.lower()
  84. while guessesleft > 1 :
  85. if playerguesslow in word:
  86. for n,i in enumerate(hangword):
  87. if i==playerguesslow:
  88. uslist[n]=playerguesslow
  89. rightletter = 'Letter "%s" is correct!\n' % playerguess
  90. print (rightletter)
  91. print('The word now looks like this:\n ')
  92. print (' '.join(uslist))
  93. print ('')
  94. sentence3 = 'You still have %d guesses left.\n' % guessesleft
  95. if uslist != hangword:
  96. print (sentence3)
  97. if uslist == hangword:
  98. print ('Congratulations..\n')
  99. break
  100. else:
  101. playerguess = input('Guess a letter..\n')
  102. playerguesslow = playerguess.lower()
  103. while playerguesslow not in alphabet or len(playerguess) > 1 or playerguess == '':
  104. playerguess = input ('That is not a letter, please try again.\n')
  105. playerguesslow = playerguess.lower()
  106. while playerguesslow in uslist:
  107. playerguess = input ('You have already chosen that letter.\n')
  108. playerguesslow = playerguess.lower()
  109. else:
  110. guessesleft = guessesleft-1
  111. wrongletter = 'There is no letter "%s" in the word.' % playerguess
  112. print (wrongletter)
  113. sentence2 = 'Too bad, you have %d guesses left..\n' % guessesleft
  114. print (sentence2)
  115. print('The word still looks like this:\n ')
  116. print (' '.join(uslist))
  117. print ('')
  118. playerguess = input('Guess again..\n')
  119. playerguesslow = playerguess.lower()
  120. while playerguesslow not in alphabet or len(playerguess) > 1 or playerguess == '':
  121. playerguess = input ('That is not a letter, please try again.\n')
  122. playerguesslow = playerguess.lower()
  123. while playerguesslow in uslist:
  124. playerguess = input ('You have already chosen that letter.\n')
  125. playerguesslow = playerguess.lower()
  126.  
  127. # End of the game
  128. if uslist == hangword:
  129. print ('You have won!\n')
  130. else:
  131. print ('I am sorry, you are game over..\n')
  132. wordnotfound = 'the word you were looking for was "%s".\n' % word
  133. print (wordnotfound)
  134. print ('Would you like to play another game?\n')
  135. qnewgame = input ('Yes or No ?\n')
  136. if qnewgame == 'Yes' or qnewgame == 'yes'or qnewgame == 'YES' or qnewgame == 'Y' or qnewgame == 'y':
  137. exec(open("./Hangman.py").read())
  138. elif qnewgame == 'No'or qnewgame == 'no'or qnewgame == 'NO' or qnewgame == 'N' or qnewgame == 'n':
  139. quit()
  140. else:
  141. qnewgame = input ('I did not quite get that. Yes or No?')
  142. if qnewgame == 'Yes' or qnewgame == 'yes'or qnewgame == 'YES' or qnewgame == 'Y' or qnewgame == 'y':
  143. exec(open("./Hangman.py").read())
  144. elif qnewgame == 'No'or qnewgame == 'no'or qnewgame == 'NO':
  145. quit()
  146. else:
  147. print ('Sorry, I still did not get that, the program will now quit.')
  148. print ('')
  149. quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement