Code and Explanation
Welcome to the second beginner project in python. My goal with this project was, to implement a simple number-guessing game, where the player would be able to insert some numbers into the console, receives some feedback whether the number to be guessed is above, below or equal to the number that he/she has entered. If the number is below or above, the player would have another shot at guessing the number correctly.
Again, basic explanations are given in the code example with „### @“
Step 1 Importing Libraries and defining some Variables
import random import numpy as np ################# # Simple Code for Guessing the Number ################# min_number = 0 ### Values can be changed to whatever you like max_number = 10 ### Values can be changed to whatever you like number_to_be_guessed = random.randint(min_number, max_number) ### The number to be guessed gets randomly initialized
Step 2 Building the main Function
def play():
print(f"Please put in your guess below (Hint: The number to be guessed is between {min_number} and {max_number}") ### Ask the user to input a number
guess = int(input()) ### Number gets transformed to an integer
want_to_play = True
while want_to_play == True:
if guess % number_to_be_guessed == 0: ### If the guess is correct, break the while loop and print a statement
print(f"Congratulations, your guess was correct!")
want_to_play = False
break
elif guess < number_to_be_guessed:
print(f"The number you are looking for is greater than {guess}") ### If the guess is smaller than the number to be guessed give a hint to the user
print(f"Do you still want to play? Yes or No?") ### Also ask the user if he wants to continue playing
decision_to_continue_playing = input().capitalize()
if decision_to_continue_playing == "Yes": ### Continue with the loop (start at the very beginning again)
print(f"Please put in your guess below (Hint: The number to be guessed is between {min_number} and {max_number}")
guess = int(input()) ### User is able to input another guess
else:
want_to_play = False
break
elif guess > number_to_be_guessed:
print(f"The number you are looking for is lower than {guess}") ### If the guess is greater than the number to be guessed give a hint to the user
print(f"Do you still want to play? Yes or No?") ### Again ask if the user wants to continue playing
decision_to_continue_playing = input().capitalize()
if decision_to_continue_playing == "Yes": ### Continue with the loop (start at the very beginning again)
print(f"Please put in your guess below (Hint: The number to be guessed is between {min_number} and {max_number}")
guess = int(input()) ### User is able to input another guess
else:
want_to_play = False
break
play() ### Don't forget to actually call the function