(Expanded) Scrabble - Working with inputs: Any Feedback?

Hello!

I’m new to coding and currently working on the Computer Science Career Path. This afternoon, I finished the “Dictionary” Module of CS101 where one task was to finish the Scrabble project. After completing the task, I decided to expand it a little bit:

The program is designed to calculate the sum of all the points of individual users. To achieve a better overview, I have divided the code in categories. An explanation for all the steps is below the code. It was really fun to combine my Python knowledge of the previous models and I’m looking forward to hearing any feedback of how I could’ve simplified this entire program. I only started the CS career path last week and didn’t have any prior coding experience. I have a lot to learn.

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 = {letter: point for letter,point in zip(letters, points)}
letters_to_points[" "] = 0


# WELCOME MESSAGE
print(10 * "*" + "\n")
print("Welcome to the Scrabble Calculator!" + "\n")
print(10 * "*" + "\n")

# USER SELECTION
print("Please enter your usernames. If all users were added, please type \"Done\"." + "\n")

number_players = 0
users = {}
user_list = []

while True:
    userinp = input("User " + str(number_players + 1) + ": ")
    user = str(userinp)
    if user != "Done":
      if user in users:
        print("Username already taken. Please select a unique username.")
        continue
      elif user == "":
        print("Username cannot be empty.")
      elif " " in user:
        print("Username has to be one word.")
      elif user not in users:
        number_players += 1
        users[user] = 0
        user_list.append(user)
        continue
    elif user == "Done":
        break

print("\n" + 10 * "*" + "\n")

welcome_message = "Welcome to Scrabble "
for i in range(len(user_list) - 1):
  welcome_message += user_list[i] + ", "
welcome_message += "and " + user_list[-1] + "!" + "\n"

print(welcome_message + "\n" + 10 * "*" + "\n")

# GAME

def score_word(word):
  point_total = 0
  for letter in word:
    point_total += letters_to_points.get(letter, 0)
  return point_total

number_rounds = 0
while True:
  print("Round " + str(number_rounds + 1) + "\n")

  for user in user_list:
    print(str(user) + " it is your turn!\n")
    wordtemp = input("Enter your word: ")
    word = wordtemp.upper()
    word_score = score_word(word)
    prev = users.get(user)
    users[user] = prev + word_score
    print("\nCongratulations, {}, {} points were added to your score.\n".format(user, word_score))
    print(10 * "-")
    continue

  print("\nDo you want to play another round?\n")

  while True:
    choice_roundtemp = input("Yes/No: ")
    choice_round = choice_roundtemp.upper()

    if choice_round != "NO" and choice_round != "YES":
      print("Please choose Yes or No")
      continue
    elif choice_round == "NO" or choice_round == "YES":
      break
  if choice_round == "YES":
    number_rounds += 1
    continue
  elif choice_round == "NO":
    print("\nThank you for playing!" + "\n" + 10 * "*" + "\n")
    break

# RESULTS

print("\n" + "Here are your scores:" + "\n")
tie_message = "\nIt's a tie! The winners are "
win_message = "\nThe winner is "

points_list = []
for user in user_list:
  points = users.get(user)
  points_list.append(points)
  print("{} got {} points.".format(user, points))

points_list_user = zip(user_list, points_list)
winning_points = max(points_list)
winner_list = []

for name in points_list_user:
  for point in name:
    if point == winning_points:
      winner_list.append(name[0])

if len(winner_list) > 1:
  for i in range(len(winner_list) -1):
    tie_message += winner_list[i] + ", "
  tie_message += "and " + winner_list[-1] +"!"
  print(tie_message)
elif len(winner_list) == 1:
  win_message += winner_list[0]
  print(win_message + "! Congratulations!")

This is how the output of the program looks like in the terminal. We have two users (chocolateflight and codecademy). Chocolateflight used Python as a word and codecademy used JavaScript. Codecademy won this round.

**********

Welcome to the Scrabble Calculator!

**********

Please enter your usernames. If all users were added, please type "Done".

User 1: chocolateflight
User 2: codecademy
User 3: Done

**********

Welcome to Scrabble chocolateflight, and codecademy!

**********

Round 1

chocolateflight it is your turn!

Enter your word: Python

Congratulations, chocolateflight, 17 points were added to your score.

----------
codecademy it is your turn!

Enter your word: JavaScript

Congratulations, codecademy, 24 points were added to your score.

----------

Do you want to play another round?

Yes/No: No

Thank you for playing!
**********


Here are your scores:

chocolateflight got 17 points.
codecademy got 24 points.

The winner is codecademy! Congratulations!

Explanation:

Click Here

# WELCOME MESSAGE
This section prints an initial welcome message just by using print() statements.

# USER SELECTION
This section uses the input feature of Python. Once all usernames were added, you can type “Done” and the program jumps to the next section. Before that, however, an additional welcome message is printed showing the added usernames. Usernames can’t be empty, they can’t contain spaces (so they must be a single word) and if the username is taken already, it is rejected as well. I decided to use a while-loop for this. Once all usernames were added, the loop breaks.

The selected usernames are added to a list (user_list) and a dictionary (using the value 0). The reason I’m using both is to simplify things down the line. This will be explained in the Game section.

# GAME
The actual game runs through a while-loop with a nested for-loop and an additional nested while-loop.

  • First, using a nested for-loop, User 1 is asked to provide a Word using another input. Using the score_word() function, the score of the word is being determined.
  • The program then takes the previous score from the dictionary (which was set to 0 in the beginning), adds the new word score, and then stores the new score in the dictionary again. By doing that, I ensure that we always have the current score stored in the dictionary - in case the users play multiple rounds. This is why I decided to use both a list and a dictionary. While the list will help me in the end to determine a winner, the dictionary is an easier to store the points.
  • Once User 1 has entered a word, the program asks User 2 to import a word, and so on until all users were able to enter a word.
  • We then leave the for-loop and enter another while-loop where the program asks if the users want to play another round. If they want to play another round, they type “yes” and the program goes back to the beginning of the main while-loop. If they want to end the game, they type “no” and the program jumps to the # RESULTS

# RESULTS

  • First, this section will print out a sentence for each user telling them how many points they got. This is done using a for-loop. At the same time, the userpoints are taken out of the dictionary and stored in a separate list. In a second step, we determine the highest point number using the max() function on this new points_list.
  • The code then zips the user_list and the points_list. A new for-loop then iterates through this new list looking for users with the highest point number.

If more than two users are found, a message announcing a tie will be printed showing the point number and the winning users. If only one user has reached the highest point number, a separate message will print announcing this user as the winner.