Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- In the game Scrabble each letter has a value. One completed word gives you a score.
- Write a program that takes a word as an imput and outputs the calculated scrabble score.
- Values and Letters:
- 1 - A, E, I, O, U, L, N, R, S, T
- 2 - D, G
- 3 - B, C, M, P
- 4 - F, H, V, W, Y
- 5 - K
- 8 - J, X
- 10 - Q, Z
- Example:
- The word "cabbage" gives you a score of 14 (c-3, a-1, b-3, b-3, a-1, g-2, e-1)
- Extensions:
- You can play a double or triple letter
- You can play a double or triple word
- """
- def check_multiple(word):
- word_list = word.split()
- word_multiplied = ""
- for words in word_list:
- n = 1 # counter
- for i in words[1:]:
- if i.isdigit() and words[n-1].isalpha(): # multiply a letter
- words = words[:n] + words[n-1]*(int(i)-1) + words[n+1:]
- n += int(i)-2 # change counter to account for longer word after multiplication
- elif i.isdigit() and words[n-1] == "*": # multiply a word
- if words[n-2].isalpha():
- words = words[:n-1]*(int(i))
- elif words[n-2].isdigit():
- words = words[:n-2]*(int(i)) # in case there is something like: hello2*3
- n += 1
- word_multiplied += words
- return word_multiplied
- # set up score system
- scores = {}
- for key in ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T']:
- scores[key] = 1
- for key in ['D', 'G']:
- scores[key] = 2
- for key in ['B', 'C', 'M', 'P']:
- scores[key] = 3
- for key in ['F', 'H', 'V', 'W', 'Y']:
- scores[key] = 4
- for key in ['K']:
- scores[key] = 5
- for key in ['J', 'X']:
- scores[key] = 8
- for key in ['Q', 'Z']:
- scores[key] = 10
- 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. ")
- while True:
- word = input("Enter the Word(s) to score: ").upper() # upper so dict works
- word = check_multiple(word) # call function to include multiple words or letters
- if word.isalpha():
- result = sum([scores[i] for i in word]) # if the input is only letters, add them together to calculate the score
- print(result)
- break
- else:
- print("Please enter valid a valid input!")
- continue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement