I made this Scrabble with care since it will be mainly used by family that will not understand the error codes so that is why I catch basically every possible error, to make it user friendly. I have revised this based on an earlier post and hope to improve its efficiency even more now that I have basically redone the code.
letter_val = {" ": 0, "A": 1, "B": 3, "C": 3, "D": 2, "E": 1, "F": 4, "G": 2, "H": 4, "I": 1, "J": 8, "K": 5, "L": 1, "M": 3, "N": 1, "O": 1, "P": 3, "Q": 10, "R": 1, "S": 1, "T": 1, "U": 1, "V": 4, "W": 4, "X": 8, "Y": 4, "Z": 10}
print("""
___| | | |
\___ \ __| __| _` | __ \ __ \ | _ \ __|
| ( | ( | | | | | | __/ |
_____/ \___|_| \__,_|_.__/ _.__/ _|\___|_|
""")
players = []
def add_players():
print("If amount of players is less than 4, press enter after all players are entered.")
while len(players) < 4:
new_player = input("Enter player to add>>> ")
if new_player:
if new_player not in (stats[0] for stats in players):
players.append([new_player, 0])
print("Player {} added.".format(new_player))
else:
print("Player already in player list.")
else:
break
add_players()
def home():
option = input('Would you like to [A]dd a score, [V]iew scores, [U]ndo the last change, or [End] the game? > ')
return option
def add_score():
score = 0
global temp_value, temp_player, undo_ind
player = temp_player = input("Enter player to score>>> ")
if player:
if player in (stats[0] for stats in players):
try:
word = input("Enter word to score(Enter a space for blank tiles)>>> ")
if word:
value = temp_value = sum(letter_val[i.upper()] for i in word)
else:
add_score()
except KeyError:
print("Word must consist of letters only.")
add_score()
dbl_or_trip = input("Is word [D]ouble or [T]riple or [N]either?> ")
if dbl_or_trip.lower() == "d":
score += value * 2
elif dbl_or_trip.lower() == "t":
score += value * 3
elif dbl_or_trip.lower() == "n":
score += value
else:
print("Please enter a valid option.")
add_score()
print("Are there any double or triple letters?(For example, if the letter \"b\" is doubled and \"t\" is tripled, it is entered like this> b2 t3)")
mult_letters = input("Enter any double or triple letters in the above format or press enter to skip> ")
spl_mult_letters = mult_letters.split()
try:
for letter, multiplier in spl_mult_letters:
for stats in players:
if stats[0] == player:
if 4 > int(multiplier) > 1:
stats[1] += letter_val[letter.upper()] * (int(multiplier) - 1) + score
break
else:
print("The multiplier must be either 2 or 3.")
except ValueError:
print("Please enter multiplied letters in the above format.")
else:
print("Player entered is not in player list.")
add_score()
undo_ind = False
main()
def view_scores():
for i in players:
print("Player %s has a score of %d" % (i[0], i[1]))
main()
def undo():
global undo_ind
no_change = "No changes have been made."
try:
if temp_value and temp_player and undo_ind is False:
for stats in players:
if stats[0] == temp_player:
stats[1] -= temp_value
else:
print(no_change)
except NameError:
print(no_change)
home()
undo_ind = True
main()
def end_game():
for name, score in players:
print("Player %s has a score of %d" % (name, score))
__name__ = "end"
def main():
option = home()
if option.lower() == "a":
add_score()
elif option.lower() == "v":
view_scores()
elif option.lower() == "u":
undo()
elif option.lower() == "end":
end_game()
else:
print("Please enter a valid option.")
main()
if __name__ == "__main__":
main()
I know I have recursion in the main()
function and I would like someone to provide an example specific to this scenario for me to use, as I can't figure out a working solution. Also the reason I set __name__
to "end" in the end_game()
function is because if I did not, end game would run the if statement at the end of the code and then repeat main()
putting the user in an endless loop that can only be escaped by CTRL+C. If anyone has a better way of the interaction between end_game()
and main()
I would like to hear and improve on it. I am fairly new to Python, and examples do help a lot for this program and future references. Thanks.