-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrabble.py
More file actions
154 lines (101 loc) · 3.91 KB
/
scrabble.py
File metadata and controls
154 lines (101 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from string import ascii_letters
def get_word():
"""
Asks the user to input a word.
Returns a string containing the user's input.
"""
user_word = input("\n\nEnter a word: ")
return user_word
def ask_user_yes_no(yes_no_question) -> bool:
"""Simplifies if/else to determine the correct answers from the user input.
Args:
yes_no_question: A string that asks user a yes or no question.
Returns:
True if the user's answer is in CHOICE_YES,
and False otherwise.
Prints a message to the user if their input are not similar
to the ones in CHOICE_YES and CHOICE_NO.
"""
CHOICE_YES = ("yes", 'y')
CHOICE_NO = ("no", 'n')
while True:
user_choice = input(yes_no_question).lower()
if user_choice in CHOICE_YES:
return True
elif user_choice in CHOICE_NO:
return False
else:
print("\nInvalid Input. Try again.")
def generate_letters():
"""
Loops through all the alphabet letters.
Splits the letters and add it into the empty list.
Returns a list of splitted alphabet letters.
"""
letters = []
for char in ascii_letters:
letters += char.split()
return letters
def generate_points():
"""
A list of integers as a point for each letter in the alphabet.
Multiplies the list by 2 to match the length of generate_letters function.
Returns the extended list of integers.
"""
points = [1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3,
4, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10] * 2
return points
def map_letters_points():
"""
Calls generate_letters function and assign the
returned value to letter_list.
Calls generate_points function and assign the returned value to point_list.
Maps each letter to point with letter being the key and point as the value.
Handles blank tiles by assigning 0 to letter_to_points
if the user did not input anything.
Returns a dictionary that has the elements of letter_list as keys
and the elements of point_list as values.
"""
letter_list = generate_letters()
point_list = generate_points()
letter_to_points = {letter: point for letter,
point in zip(letter_list, point_list)}
letter_to_points[" "] = 0
return letter_to_points
def score_word(word):
"""
Calls map_letters_points function and assign the
returned value to letter_points.
Sets point_total equals to 0
Iterates through the letters in word and adds the point value of
each letter to point_total from letter_points dictionary.
Adds 0 to point_total if the letter is not found in letter_points.
Returns an integer of the total points that the
user has scored with the give word.
"""
letter_points = map_letters_points()
total_point = 0
for key in word:
total_point += letter_points.get(key, 0)
return total_point
def play_word():
"""
Gets a word input from the user.
Sums the point of each letter from the given word.
Pluralizes the word "point" if the scored_points is
either 0 or more than 1.
Asks the user if they want play again.
Asks the user to input word if the user wants to play again.
Exits the program if the user does not want to continue with the program.
"""
while True:
player_word = get_word()
scored_points = score_word(player_word)
if scored_points == 1:
print(f"\nYour word scores {score_word(player_word)} point!\n")
else:
print(f"\nYour word scores {score_word(player_word)} points!\n")
if not ask_user_yes_no("\nWould you like to play again? (Y/N): "):
print("\n\nExiting program...\n\n")
break
play_word()