Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- # List of words to use in the game
- words_list = ["python", "programming", "challenge", "developer", "scramble", "keyboard", "function", "variable"]
- # Function to scramble a word
- def scramble_word(word):
- scrambled = list(word)
- random.shuffle(scrambled)
- return ''.join(scrambled)
- # Function to save scores to a file
- def save_score(player_name, score):
- with open("scores.txt", "a") as file:
- file.write(f"{player_name}: {score}\n")
- # Function to read and display scores from a file
- def display_scores():
- print("\nHigh Scores:")
- try:
- with open("scores.txt", "r") as file:
- scores = file.readlines()
- for line in scores:
- print(line.strip())
- except FileNotFoundError:
- print("No scores recorded yet.")
- # Main game logic
- def play_game():
- player_name = input("Enter your name: ")
- score = 0
- print("\nWelcome to the Word Scramble Game!")
- print("Unscramble the words and type your answer.\n")
- for word in words_list:
- scrambled = scramble_word(word)
- print(f"Scrambled word: {scrambled}")
- guess = input("Your guess: ")
- if guess.lower() == word:
- print("Correct!\n")
- score += 1
- else:
- print(f"Wrong! The correct word was: {word}\n")
- print(f"Game over! Your score is: {score}")
- save_score(player_name, score)
- display_scores()
- # Run the game
- if __name__ == "__main__":
- play_game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement