Welcome back to my blog for this month's post! Somehow, I've just realized that I didn't need to rewrite the post titles each time! 😅 Previews are a lot more valuable than I initially thought... Anyway, this is my third and FINAL post in what ended up becoming my Guess the Number series. If you would like a re-cap for my previous Guess the Number posts, here is Project 2 and Project 3.
As you may have guessed from the title, I will talk about how I implemented a "game over" after a limited number of guesses. My previous posts introduced codes that would allow users to make guesses as to what number the program has chosen, but the user was given an unlimited number of guesses. Many people who have ever played games would know that a game with no changes in difficulty, limitations or consequences can become quite dull after a few playthroughs. In this final instalment of Guess the Number, I will introduce two codes that give the user three guesses, and if the user gets them all wrong, the game will declare a game over.
I won't be introducing any new types of functions and operators this time, but rather use nested if statements and add extra lines defining the conditions. There are two versions of the updated game. In both versions, if a number below 1 or above 10 was entered, the response will be an error message suggesting that they have guessed a number outside of the range. However, in the first version the user will lose a guess if they type in a number outside of the range and in the second version they will not. I decided to write about both versions of the game in this post because I found it interesting that despite the first version arguably being the harder game, it was actually the easier code to plan for.
First version code: You can view the code below by either downloading the PDF from the link or by looking at the image posted here.
- c=3
- Allows Python to define c as 3
- The user has three chances to guess the correct number
- c=c-1
- Every time the loop is repeated, the new c is minus 1 of the old c
- The user originally has three chances. If they get one guess wrong, they lose one chance and is left with two chances. If they get another guess wrong, they lose another chance and is left with one chance...
- if c>0
- As long as the user hasn't run out of chances, they will receive a statement regarding their input (and the number of chances they have left)
- else
- In this case, if c is 0 or smaller
- When the user runs out of guesses, they will receive a "GAME OVER"
Second version code: You can view the code below by either downloading the PDF from the link or by looking at the image posted here.
- c=c+1
- Before the loop c=3
- During the first loop c=2 because c=c-1
- But when the user types in a number lower than 1 and above 10, c=3 again because c=(c-1)+1
No comments:
Post a Comment