############################################################ # COMPUTER SCIENCE UNDERGRADUATE SCHOLARSHIP EXAM # UNIVERSITY OF WAIKATO # PYTHON PROGRAMMING LANGUAGE # JULIANO TAVARES (TAURANGA BOYS' COLLEGE YR 13) ############################################################ # Importing necessary modules import random # Welcome user to the game print("Welcome to Animal Wordle!") # List of animals for the game animals = ["duck", "goat", "bear", "lion", "frog", "deer", "swan", "wolf", "crab"] # Get a random animal from the list above random_animal = random.choice(animals) # Set variables (guesses) max_guesses = 6 current_guess = max_guesses # Provide instructions and game history print(''' Wordle is a word game that was created and developed by Welsh software engineer Josh Wardle. In the original version of Wordle, players have six attempts to guess a five-letter word, with feedback given for each guess in the form of coloured tiles indicating when letters match or occupy the correct position. In Animal Wordle, players have six attempts to guess a four-letter animal, with feedback given for each guess indicating when letters match or occupy the correct position. If the character is in the correct position, it is indicated with a hash (#). If a character is correct, but in the wrong position, it is indicated with a hyphen (-). If a character is incorrect, it is represented with a full stop (.). Feedback will indicate: - Correct letter and position: # - Correct letter but wrong position: - - Incorrect letter: . ''') # Store the user's name name = input("Please enter your name: ") # Get info on whether user wants to play or not play = input(f"Hi {name}! Would you like to play Animal Wordle? [y/n]: ").strip().lower() # Play if user wants to play or quit if user doesn't want to play if play != "y": print("No Problem, have a great rest of your day!") quit() # Run the game itself while current_guess > 0: # print the number of guesses the user has available print(f"\nYou have {current_guess} guesses left.") # list of guesses guess = [] # User enters each of the 4 letters to form the word # check if word is correct and if input is valid for i in range(1, 5): while True: letter = input(f"Enter letter {i}: ").strip().lower() if len(letter) == 1 and letter.isalpha(): guess.append(letter) break else: print("Invalid input. Please enter a single alphabetic character.") guess_word = ''.join(guess) # If the word is correct, user wins if guess_word == random_animal: print("Result: ####") print("Woohoo! You guessed the word correctly!") break # If word is wrong, add guesses and restart else: result = [] for index, letter in enumerate(guess): if letter == random_animal[index]: # Correct position result.append('#') elif letter in random_animal: # Correct letter but wrong position result.append('-') else: # Incorrect letter result.append('.') print("Result:", ''.join(result)) print("Getting there! Keep guessing!") current_guess -= 1 # If user runs out of guesses, user loses if current_guess == 0: print("You have run out of guesses! The correct word was:", random_animal)