Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. import random
  2.  
  3. difficulty = {1: [4, 5, 6], 2: [7, 8],
  4. 3: [9, 10, 11], 4: [12, 13],
  5. 5: [14, 15]
  6. }
  7. #Keys are the various word lengths for each difficulty (1-5).
  8. acceptedWord = []
  9. #Start a blank list, used in getWords()
  10.  
  11. def getWords():
  12. while True:
  13. try:
  14. level = int(input("What difficulty would you like? (1,2,3,4,5) "))
  15. break
  16. except (ValueError, KeyError):
  17. print("That is not a number")
  18. #Take a numerical input from the user, attempt to parse it, if it fails, carry on the loop.
  19. num = random.choice(difficulty[level])
  20. #num (used to store word lengths) is a random choice from the available lengths for the selected difficulty.
  21. with open('wordbank.txt', 'r') as file:
  22. #Open the file containing word-bank
  23. for x in map(str.upper, map(str.strip, file.readlines())):
  24. #loop over the stripped down words
  25. if len(x) == num:
  26. acceptedWord.append(x)
  27. #If length of word is equal to random choice from suitable lengths, add it to a list.
  28.  
  29. #Random index from total number of items in list.
  30. answers = random.sample(acceptedWord, num)
  31. #picks a selection of the available words. Amount of words = word length.
  32. trueWord = answers[random.randint(0, len(answers)-1)]
  33. #trueWord = answer. Random item of the shortlisted avaliable words
  34. print('n'.join(answers))
  35. #Prints hints.
  36. game(trueWord)
  37.  
  38.  
  39. def game(trueWord):
  40. for x in range(0,5):
  41. #The user has 5 guesses
  42. print("Guesses Left: ", 5-x)
  43. userInput = input("Guess: ").upper()
  44. #All guesses/inputs are parsed to uppercase.
  45. userList = list(userInput)
  46. #List of the letters the user inputed
  47. trueList = list(trueWord)
  48. #List of letter of
  49. if userInput == trueWord:
  50. print("Well done, you guessed correctly")
  51. getWords()
  52. #If the user enters the correct word, quit the program
  53. correctGuess = 0
  54. for item in list(userList):
  55. #for each letter the user inputed
  56. if item == trueList[userList.index(item)]:
  57. #if the letter is in the same position as the answer
  58. correctGuess += 1
  59. #increment the correct amount of letters by 1
  60. print(correctGuess, "out of ", len(trueList), "correct.")
  61. print("Bad luck! The answer was: ", trueWord)
  62. getWords()
  63.  
  64. getWords()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement