Ayesha wants you to create a guess the space word game.
Open Q06 in your IDE.
The starter code contains an array of space-related words (planets, celestial bodies, etc.).
It also contains a function that randomly selects a word from the array. This is the secret word the user needs to guess.
Ayesha wants the program to:
Your program must include at least two subprograms that you have written yourself.
You must include comments in the code to explain the logic of your solution.
Save your code as Q06FINISHED with the correct file extension for your programming language.
| Secret word: mars | ||
|---|---|---|
| Attempt | First | Second |
| Input | moon | star |
| Correct store | m | m, s, a, r |
| Wrong store | o, n | o, n, t |
FINAL ANSWER: A complete program that fulfills all functional requirements including comments and at least two custom subprograms. Example Python implementation:
# Space Word Guessing Game
import random
# Starter code array and selection function
words = ["mars", "comet", "saturn", "orbit", "venus", "pulsar"]
def get_secret_word():
return random.choice(words)
# Subprogram 1: Get validated input from the user
def get_valid_guess(word_length):
valid = False
while not valid:
guess = input(f"Enter your {word_length}-letter guess: ").lower()
if len(guess) == word_length:
valid = True
else:
print(f"Invalid length! Please enter a word with exactly {word_length} letters.")
return guess
# Subprogram 2: Process letters and update unique stores
def update_stores(guess, secret, correct_list, wrong_list):
for letter in guess:
if letter in secret:
if letter not in correct_list:
correct_list.append(letter)
else:
if letter not in wrong_list:
wrong_list.append(letter)
# Main Game Loop
def play_game():
secret_word = get_secret_word()
secret_len = len(secret_word)
max_attempts = secret_len + 2
attempts_taken = 0
correct_store = []
wrong_store = []
print(f"Welcome to the Space Word Guess Game!")
print(f"The secret word has {secret_len} letters.")
guessed_correctly = False
while attempts_taken < max_attempts and not guessed_correctly:
attempts_left = max_attempts - attempts_taken
print(f"\nAttempts remaining: {attempts_left}")
# Call subprogram 1 for validated input
user_guess = get_valid_guess(secret_len)
attempts_taken += 1
if user_guess == secret_word:
guessed_correctly = True
else:
# Call subprogram 2 to update the feedback stores
update_stores(user_guess, secret_word, correct_store, wrong_store)
print(f"Incorrect guess!")
print(f"Correct letters found: {', '.join(correct_store)}")
print(f"Wrong letters found: {', '.join(wrong_store)}")
if guessed_correctly:
print(f"\nCongratulations! You guessed the word '{secret_word}' in {attempts_taken} attempts!")
else:
print(f"\nGame Over! You ran out of attempts. The secret word was '{secret_word}'.")
# Run the game
play_game()