Advertisement
superpawko

Untitled

Sep 29th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. def playHand(hand, wordList, n):
  2. """
  3. Allows the user to play the given hand, as follows:
  4.  
  5. * The hand is displayed.playHand period (the string ".")
  6. to indicate they're done playing
  7. * Invalid words are rejected, and a message is displayed asking
  8. the user to choose another word until they enter a valid word or "."
  9. * When a valid word is entered, it uses up letters from the hand.
  10. * After every valid word: the score for that word is displayed,
  11. the remaining letters in the hand are displayed, and the user
  12. is asked to input another word.
  13. * The sum of the word scores is displayed when the hand finishes.
  14. * The hand finishes when there are no more unused letters or the user
  15. inputs a "."
  16.  
  17. hand: dictionary (string -> int)
  18. wordList: list of lowercase strings
  19. n: integer (HAND_SIZE; i.e., hand size required for additional points)
  20.  
  21. """
  22. # BEGIN PSEUDOCODE <-- Remove this comment when you code this function; do your coding within the pseudocode (leaving those comments in-place!)
  23. # Keep track of the total score
  24. score = 0
  25. word = ""
  26. # As long as there are still letters left in the hand:
  27. while len(hand.keys()) > 0:
  28. # Display the hand
  29. print("Current Hand:", end=" ")
  30. displayHand(hand)
  31. # Ask user for input
  32. word = input('Enter word, or a "." to indicate that you are finished: ')
  33. # If the input is a single period:
  34. if word == ".":
  35. # End the game (break out of the loop)
  36.  
  37. break
  38.  
  39. # Otherwise (the input is not a single period):
  40. else:
  41. # If the word is not valid:
  42. if not isValidWord(word, hand, wordList):
  43. # Reject invalid word (print a message followed by a blank line)
  44. print("Invalid word, please try again.")
  45. print("")
  46. # Otherwise (the word is valid):
  47. else:
  48. # Tell the user how many points the word earned, and the updated total score, in one line followed by a blank line
  49. word_score = getWordScore(word, n)
  50. score += word_score
  51. print("{} earned {} points. Total: {} points." .format(word, word_score, score))
  52. print("")
  53. # Update the hand
  54. hand = updateHand(hand, word)
  55. #
  56.  
  57. if word == ".":
  58. print("Goodbye! Total score: {} points.".format(score))
  59. else:
  60. # Game is over (user entered a '.' or ran out of letters), so tell user the total score
  61. print("Run out of letters. Total score: {} points." .format(score))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement