16amattice

Python Help

Dec 17th, 2022
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | Source Code | 0 0
  1. import collections
  2.  
  3. def is_valid_word(word, starting_word):
  4.     # Check if the word is long enough
  5.     if len(word) < 3:
  6.         return False
  7.    
  8.     # Check if the word is made up of only letters in the starting word
  9.     letter_counts = collections.Counter(word)
  10.     for letter, count in letter_counts.items():
  11.         if letter not in starting_word or starting_word.count(letter) < count:
  12.             return False
  13.    
  14.     return True
  15.  
  16. def get_word_from_user(n):
  17.     word = input(f"Enter a word with at least {n} characters: ")
  18.     # Convert the word to lowercase to handle case-insensitive input
  19.     return word.lower()
  20.  
  21. def play_wordbuilder():
  22.     starting_word = get_word_from_user(6)
  23.    
  24.     score = 0
  25.     used_words = []
  26.     while True:
  27.         word = get_word_from_user(3)
  28.         if is_valid_word(word, starting_word):
  29.             if word in used_words:
  30.                 print("You have already entered this word. Please enter a new word.")
  31.             else:
  32.                 used_words.append(word)
  33.                 score += 1
  34.         else:
  35.             print("Invalid word. Please try again.")
  36.        
  37.         print(f"Your current score is {score}.")
  38.  
  39. play_wordbuilder()
  40.  
Advertisement
Add Comment
Please, Sign In to add comment