Guess the number (2/25)
Table of contents
Guess the number vs computer
Simple small program, and I suppose it's often used to get to know the languages you are studying. I also like this program, it can be pure simple, but you can also get your hands dirty, with validation, function loops etc.
Pseudocode
Here is my overall pseudocode for the project.
"""
computerGuess = Random number between 0-10
input player guess
Transform to integer
Check if between 0-10
Create computerGuess
Randoom number between 0-10 Store it.
Check if is == to computerGuess
Player wins
If not
lowver ask for new number with message of hotter
if not
Higher ask for new number with message of colder
Guess correct start new game
Play up to 5 before exit game.
"""
As you can see from the start il set the barrier higher than the actual needed for this project. So what do I mean by that? Well from the start I decided to make the game to be 5 guesses long, and implement some validation.
The actual program
I will not list the whole program here but show some small snips.
First of all, I wanted to make a validation of the playerś input:
playerGuess = int(input("Please write a number between 0-10 "))
if playerGuess > 10 or playerGuess < 0:
print("Ohh looks like your number is not between 0-10 ")
As you can see, after I got the input I check if its greater than 10 or less than 0, if it's true, the program will quit()
with a prompt of the input is not a valid number. (also covers if the player enter a string)
But when I started to make the game loop, I fast realised that I need to be able to recall the player guess each time, the guess was not correct, so I made it into a function:
def getPlayerGuess():
getPlayerGuessStatus = True
while getPlayerGuessStatus == True:
playerGuess = input("Please write a number between 0-10 ")
try:
print('Ohh it looks like you wrote a number')
playerGuess = int(playerGuess)
if playerGuess > 10 or playerGuess < 0:
continue
else:
getPlayerGuessStatus = False
print(f'Here it is :) {playerGuess}')
except:
print("Ohh did you not enter a number?")
print(playerGuess)
continue
return(playerGuess)
print(getPlayerGuess())
With this one, the program can simply just call for a new guess, and the function will run, take care of validation and return a new guess.
The computer guess is generated using the build in random.randint()
with min augment of 0 and max of 10.
Next up I made the game loop:
gameScore = True
computerGuess = random.randint(0,10)
print(computerGuess)
gameScore = 0
while gameScore < 5:
playerGuess = getPlayerGuess()
if playerGuess < computerGuess:
print("Ohh you are close are you, try a bit warmer")
continue
elif playerGuess > computerGuess:
print("Ohh close one, try a bit colder")
continue
elif playerGuess == computerGuess:
print("BOOOOM you nailed it GOOD JOB")
gameScore = gameScore+1
print(f'Score: {gameScore}')
computerGuess = random.randint(0,10)
print(computerGuess)
print('And you are the BIG WINNER!!!!!')
Well, this is the last version of the game loop. Sadly I did not document the first game loop. But basically, the game starts by setting the game score to 0. generating a computer guess and then asking for a player guess. when the playerGuess returns it, will run an if...else statement to check if the player's guess is higher, lower or the same as the computer generated. If the player is not guessing correctly, it will keep running the loop until the player is spot on, then raise the game score by 1 and starts a new round, by first making a new computer guess.
Finally, when the playerScore hit 5 successful guesses, the program is ending with a nice small prompt.
Learning
Yes, it is a simple game, and of course, I could put alot more work in it to finish it. But this is not about finishing the program for release, it's all about getting my hands dirty, and keeping my self entertained while learning.
Endless loop :( I come across a few endless loops. A thing that also could happen in VBA(you don't want your Excel file frozen), but not a big issue when I played around with Javascript. Thankfully I could just close the terminal that was running the Python script.
Missing comment :( I'm writing this the day after I made the program, and can see my missing comment or can you see things there is not there??? I should get used to also type comments even in small programs/scripts.
Overall a good practice, got a lot of code in my hands, and also some good learnings, problems to crack, and some bugs and error messages.
Enjoy
Mattias