- November 13, 2022
- Comments Disabled
Introduction
The purpose of carrying out these mini projects was to re-familiarise myself with Python scripting, without needing to resort back to the basics. What’s good about these mini projects is that they utilise a lot of the core concepts that I have used and continue to use with Python as it relates to AWS. Namely, modules, functions, script linking and conditionals.
Project 1: Guess the Number
This project involved creating a guessing game which was twofold. The first part of the game allowed the user to guess a random number generated by the computer. A function is created which uses the random module to generate a random number between 1 and (x). The parameter (x) is substituted in when the function is called. For example if the parameter was (10) the computer would generate a random number between 1 and 10 for the user to guess. A while loop, followed by a conditional which lets the user know if there guess is too high or low. This loop will continue to run until the number is guessed.
The second part of this project is in reverse of the first. This time the user will think of a random number and the computer will try to guess it. A while loop was created which generates a random guess for the computer and then prompts the user to tell the computer if the guess was correct, too high, or too low. Depending on whether the guess is too high or low the next guess from the computer, the limit of the random selection will increase or decrease. For example, if the starting range was between 1-50 and the first guess from the PC was 38, if that guess was too high the PC would know the correct number is between 1-37. Therefore, everything past 38 would now be ignored. This loop continues until the correct number is guessed. I also added a (.lower) parameter on the input string to account for users entering the responses in upper case letters.
Project 2: Rock Paper Scissors
This mini project involves creating a game rock paper scissors between the user and computer within Python. The script is comprised of two functions. The first is called play “play()”, which prompts the user for an option and generates a random selection for the computer. This function also checks if there is a tie between the user an computer and returns that result.. The second function “is_win()” checks all the winning possibilities for the player and returns True if any of them are met. All other possibilities either result in a tie, or the user loosing against the computer.
Project 3: Hangman
This project was a bit more challenging and involved a few more moving parts. The game resembles typical hangman whereby the user aims to guess a random word generated by the computer. To begin, I sourced a list of the most frequently used words within the English language. I created a separate Python file “words.py” to store these in, and referenced the file within the main project file. Other decencies for this project included were “string” and “random”. The first function I wrote “get_valid_word()” was one that could extract a random valid/suitable word from the “words.py” file. This function ignored words containing any special characters including spaces. This word was then assigned to the “word” variable.
The next function “hangman() was where the bulk of the game was created. I defined some initial variables for use within the game. This included:
- -The random word selected from the “words.py” file (word).
- -A variable to count the number of letters in the selected word (word_letters).
- -Setting the letters to uppercase so there is no logical issues with casing (alphabet).
- -A variable to store the letters that the user has already used (used_letters).
- -An input statement so the user could guess a letters (user_letter).
- -The number of lives the user has left (lives), this was set to seven.
I created a while loop within the function so the guessing process could continue to run until one of two conditions were met. The user runs out of lives, or they guess the word. As the user attempts to guess the word, various actions are being carried out within the while loop. These include, appending any used letters to a list so the user can see what they have used, replacing the dashes of the random word with the correctly guessed letter, checking to see if characters that are entered are valid (so no special characters), and reduce lives each time an incorrect letter is guessed. I also added print statements to display some of this information to the user so they can monitor their progress.