|
| 1 | +import random |
| 2 | + |
| 3 | + |
| 4 | +WORDS = [ |
| 5 | + ("python", "A friendly programming language used across this project."), |
| 6 | + ("variable", "A named place where a program stores a value."), |
| 7 | + ("function", "Reusable code that performs one clear task."), |
| 8 | + ("keyboard", "The device you use to type your answers."), |
| 9 | + ("algorithm", "A step-by-step method for solving a problem."), |
| 10 | + ("database", "An organized collection of information."), |
| 11 | + ("developer", "Someone who builds software."), |
| 12 | + ("network", "Connected computers that can share data."), |
| 13 | + ("software", "Programs and apps that run on a computer."), |
| 14 | + ("internet", "The global network that connects websites and services."), |
| 15 | +] |
| 16 | + |
| 17 | + |
| 18 | +def scramble_word(word): |
| 19 | + letters = list(word) |
| 20 | + scrambled = word |
| 21 | + |
| 22 | + while scrambled == word and len(word) > 3: |
| 23 | + random.shuffle(letters) |
| 24 | + scrambled = "".join(letters) |
| 25 | + |
| 26 | + return scrambled |
| 27 | + |
| 28 | + |
| 29 | +def play_round(word, hint, score, streak): |
| 30 | + attempts = 3 |
| 31 | + scrambled = scramble_word(word) |
| 32 | + |
| 33 | + print("\n" + "-" * 50) |
| 34 | + print(f"Scrambled word: {scrambled.upper()}") |
| 35 | + print(f"Hint: {hint}") |
| 36 | + print(f"Attempts: {attempts}") |
| 37 | + |
| 38 | + while attempts > 0: |
| 39 | + guess = input("\nYour guess: ").strip().lower() |
| 40 | + |
| 41 | + if not guess.isalpha(): |
| 42 | + print("Please enter letters only.") |
| 43 | + continue |
| 44 | + |
| 45 | + if guess == word: |
| 46 | + points = attempts * 10 + streak * 5 |
| 47 | + print(f"Correct! The word was {word.upper()}.") |
| 48 | + print(f"You earned {points} points.") |
| 49 | + return score + points, streak + 1 |
| 50 | + |
| 51 | + attempts -= 1 |
| 52 | + if attempts > 0: |
| 53 | + print(f"Not quite. Attempts left: {attempts}") |
| 54 | + |
| 55 | + print(f"Out of attempts! The word was {word.upper()}.") |
| 56 | + return score, 0 |
| 57 | + |
| 58 | + |
| 59 | +def main(): |
| 60 | + print("=" * 50) |
| 61 | + print("WELCOME TO WORD SCRAMBLE GAME") |
| 62 | + print("=" * 50) |
| 63 | + print("Unscramble each word. You get 3 attempts per round.") |
| 64 | + print("Type 'quit' between rounds to stop playing.") |
| 65 | + |
| 66 | + score = 0 |
| 67 | + streak = 0 |
| 68 | + words = WORDS[:] |
| 69 | + random.shuffle(words) |
| 70 | + |
| 71 | + while True: |
| 72 | + if not words: |
| 73 | + words = WORDS[:] |
| 74 | + random.shuffle(words) |
| 75 | + |
| 76 | + word, hint = words.pop() |
| 77 | + score, streak = play_round(word, hint, score, streak) |
| 78 | + |
| 79 | + print("\n" + "=" * 50) |
| 80 | + print(f"Score: {score}") |
| 81 | + print(f"Current streak: {streak}") |
| 82 | + print("=" * 50) |
| 83 | + |
| 84 | + choice = input("\nPress Enter for next word or type 'quit': ").strip().lower() |
| 85 | + if choice == "quit": |
| 86 | + break |
| 87 | + |
| 88 | + print("\nThanks for playing Word Scramble!") |
| 89 | + print(f"Final score: {score}") |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + main() |
0 commit comments