Advertisement
Guest User

habitica_challenge_10

a guest
Oct 19th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. In the game Scrabble each letter has a value. One completed word gives you a score.
  5.  
  6. Write a program that takes a word as an imput and outputs the calculated scrabble score.
  7.  
  8. Values and Letters:
  9. 1 - A, E, I, O, U, L, N, R, S, T
  10. 2 - D, G
  11. 3 - B, C, M, P
  12. 4 - F, H, V, W, Y
  13. 5 - K
  14. 8 - J, X
  15. 10 - Q, Z
  16.  
  17. Example:
  18. The word "cabbage" gives you a score of 14 (c-3, a-1, b-3, b-3, a-1, g-2, e-1)
  19.  
  20. Extensions:
  21. You can play a double or triple letter
  22. You can play a double or triple word
  23.  
  24. """
  25.  
  26. def check_multiple(word):
  27.     word_list = word.split()        
  28.     word_multiplied = ""
  29.    
  30.     for words in word_list:
  31.         n = 1                                                           # counter
  32.         for i in words[1:]:
  33.             if i.isdigit() and words[n-1].isalpha():                    # multiply a letter
  34.                 words = words[:n] + words[n-1]*(int(i)-1) + words[n+1:]
  35.                 n += int(i)-2                                           # change counter to account for longer word after multiplication
  36.             elif i.isdigit() and words[n-1] == "*":                     # multiply a word
  37.                 if words[n-2].isalpha():
  38.                     words = words[:n-1]*(int(i))
  39.                 elif words[n-2].isdigit():
  40.                     words = words[:n-2]*(int(i))                        # in case there is something like: hello2*3
  41.             n += 1
  42.         word_multiplied += words
  43.     return word_multiplied
  44.  
  45.  
  46. # set up score system
  47. scores = {}
  48. for key in ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T']:
  49.     scores[key] = 1
  50. for key in ['D', 'G']:
  51.     scores[key] = 2
  52. for key in ['B', 'C', 'M', 'P']:
  53.     scores[key] = 3
  54. for key in ['F', 'H', 'V', 'W', 'Y']:
  55.     scores[key] = 4
  56. for key in ['K']:
  57.     scores[key] = 5
  58. for key in ['J', 'X']:
  59.     scores[key] = 8
  60. for key in ['Q', 'Z']:
  61.     scores[key] = 10
  62.    
  63. print("Rules: To multiply letters add the number after the letter (like: c3abbage), to multiply a word insert a *2 or *3 after the word (like: hello*3). To enter more than one word separate them by spaces. ")
  64.  
  65. while True:
  66.     word = input("Enter the Word(s) to score: ").upper()    # upper so dict works
  67.     word = check_multiple(word)                             # call function to include multiple words or letters
  68.     if word.isalpha():
  69.         result = sum([scores[i] for i in word])             # if the input is only letters, add them together to calculate the score
  70.         print(result)
  71.         break  
  72.     else:
  73.         print("Please enter valid a valid input!")
  74.         continue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement