Advertisement
DrAungWinHtut

scramble.py

Nov 2nd, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.53 KB | None | 0 0
  1. import random
  2.  
  3. # List of words to use in the game
  4. words_list = ["python", "programming", "challenge", "developer", "scramble", "keyboard", "function", "variable"]
  5.  
  6. # Function to scramble a word
  7. def scramble_word(word):
  8.     scrambled = list(word)
  9.     random.shuffle(scrambled)
  10.     return ''.join(scrambled)
  11.  
  12. # Function to save scores to a file
  13. def save_score(player_name, score):
  14.     with open("scores.txt", "a") as file:
  15.         file.write(f"{player_name}: {score}\n")
  16.  
  17. # Function to read and display scores from a file
  18. def display_scores():
  19.     print("\nHigh Scores:")
  20.     try:
  21.         with open("scores.txt", "r") as file:
  22.             scores = file.readlines()
  23.             for line in scores:
  24.                 print(line.strip())
  25.     except FileNotFoundError:
  26.         print("No scores recorded yet.")
  27.  
  28. # Main game logic
  29. def play_game():
  30.     player_name = input("Enter your name: ")
  31.     score = 0
  32.    
  33.     print("\nWelcome to the Word Scramble Game!")
  34.     print("Unscramble the words and type your answer.\n")
  35.    
  36.     for word in words_list:
  37.         scrambled = scramble_word(word)
  38.         print(f"Scrambled word: {scrambled}")
  39.        
  40.         guess = input("Your guess: ")
  41.        
  42.         if guess.lower() == word:
  43.             print("Correct!\n")
  44.             score += 1
  45.         else:
  46.             print(f"Wrong! The correct word was: {word}\n")
  47.    
  48.     print(f"Game over! Your score is: {score}")
  49.     save_score(player_name, score)
  50.     display_scores()
  51.  
  52. # Run the game
  53. if __name__ == "__main__":
  54.     play_game()
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement