Skip to content
MathsGenie logo
Open app

Course home

  1. IGCSE
  2. Computer Science Edexcel
  3. Question bank

Develop code

EasyMediumHard
123456789101112
Question 4

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:

  • Calculate the number of attempts the user has to guess the secret word. The maximum number of attempts must be the length of the secret word + 2. For example, the user has 6 attempts to guess when the secret word is mars (length 4).
  • Keep track of letters from incorrect attempts that are in the secret word (correct letters store) and those that are not (wrong letters store). There must be no duplicated letters in either store.
  • Display a message telling the user:
    • the number of letters in the secret word
    • how many attempts they have left
  • Force the user to input a guess that is the exact same length as the secret word.
  • Check whether the input word matches the secret word:
    • if the words match, then a victory message is displayed that includes the secret word and the number of attempts taken to guess it
    • if the words do not match, then:
      • letters from the attempt that appear in the secret word should be added to the correct letters store
      • letters from the attempt that do not appear in the secret word should be added to the wrong letters store
      • the contents of both correct and wrong letter stores are displayed
  • Allow the user another attempt until they have guessed the word or have run out of attempts
  • Display a "game over" message telling the user the game is over and revealing the secret word if the maximum attempts have been taken and the word has not been guessed.

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.

Example Trace

Secret word: mars
AttemptFirstSecond
Inputmoonstar
Correct storemm, s, a, r
Wrong storeo, no, 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()
[20]

Develop code Questions

  1. IGCSE
  2. /Computer Science
  3. /Develop code