Hello, since i am a beginner i could use some feedback on my code in order to know if i’m more or less on a good track. So i don’t have a specific question but would like to hear where i could improve my Scrabble project (in which i got a bit carried away). I didn’t learn about classes yet.
Hopefully somebody finds the time to look look at it, thanks in advance!
letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z"]
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]
letters_to_points = {key: value for key, value in zip(letters, points)}
letters_to_points[" "] = 0
player_to_words = {
"player1": ["BLEU", "TENNIS", "EXIT"], "wordNerd": ["EARTH", "EYES", "MACHINE"],
"Lexi Con": ["ERASER", "BELLY", "HUSKY"], "Prof Reader": ["ZAP", "COMA", "PERIOD"]
}
# ask input for the game
def play():
input_player = input("What is your name: ")
input_word = input("Enter a word: ")
return play_word(input_player, input_word)
# ask player to play again or not
# and show end score or not
def play_again(input_player):
play_or_not = input("play again? (y)es or (n)o: ")
if play_or_not == "n":
end_score_or_not = input("would you like to see your end score? (y)es or (n)o: ")
if end_score_or_not == "y":
print("your end score is:", str(end_score(input_player)), "\nhooray!")
elif end_score_or_not == "n":
print("okay, bye now!")
else:
print("'" + end_score_or_not + "'" + " is the wrong input. bye now! ")
elif play_or_not == "y":
input_word = input("Enter a word: ")
return play_word(input_player, input_word)
else:
print("'" + play_or_not + "'" + " is the wrong input ")
play_again(input_player)
# calculate score per word
def score_word(word):
point_total = 0
for letter in word:
point_total += letters_to_points.get(letter, 0)
return point_total
# calculate score player
def end_score(input_player):
total = 0
for words in player_to_words[input_player]:
total += score_word(words)
print(str(words), ":", str(score_word(words)))
return total
# the game
def play_word(input_player, input_word):
input_word = input_word.upper()
if not input_player.isalpha() or not input_word.isalpha():
print("only letters are allowed!")
play()
if input_player not in player_to_words:
# create new key:value pair.
# the value (input_word) must be in a list in order to be able to add more values to it.
player_to_words[input_player] = [input_word]
else:
# append new word to word-values-list for existing player
player_to_words[input_player].append(input_word)
return play_again(input_player)
# start of the game
play()