Python Exercise 001 - Guess The Number Game With For Loop

in #guess-thenumber6 years ago (edited)

In this exercise, you will be making “Guess The Number” game yourself with the help of for loop in Python. The idea is simple, we will generate a random number with the help of randint() function from 1 to 100 and ask the user to guess it. After every guess, the user will be told if the number is above or below the randomly generated number. The user will win if they guess the number in five attempts.

First of all, we have to import the random module so that we can use randint() function to generate a random number.

 
import random
 

Now, we will ask the user to enter their name using the input() function and then storing it in the player_name variable.

print("Hello, please enter your name.")
player_name = input()
 

The next step is to generate a random number using randint() function. Since randint() is one of the functions inside the random module, we must call it with random.randint() and then storing it in the secret_number variable. For this program, a random number would be generated between 1 and 100 like this.

secret_number = random.randint(1,100)
 

Welcoming the user and printing the instructions using the print() function.

print("Thanks, " + player_name + ". Please enter any number between 1 and 100. Remember, you have only 5 attempts")
 

Now we will instruct the user to enter their number (guess) and store it in the guess variable. Note that the input() function always returns a string, so we have to convert it into integer using the int() function. Using for loop and the range() function, we will restrict the user to 5 attempts. Whenever the user makes a wrong guess, for loop will start over and ask the user to guess the number again. Since the user has only 5 attempts, this block of code will stop after the fifth attempt and jump over to the next one. Similarly, if the user is able to guess the number correctly, the break statement will immediately take us out from the for block.

for total_guesses in range(5):
    guess = int(input())    
    if guess < secret_number:
        print("Your guess is below the number, please guess again")
    elif guess > secret_number:
              print("Your guess is above the number, please guess again")
    else: 
        break
 

Once the execution in for loop starts either because it has looped five times or the user was able to guess the number correctly, the next step is to print the results. This block of code will execute if the user guessed the number correctly.

 
if guess == secret_number:
    total_guesses = str(total_guesses + 1)
    print(player_name + ", it took you " + total_guesses + " attempts to guess the number")
 

And if the user runs out of their guesses (5), this block of code will execute.

 
if guess != secret_number:
    secret_number = str(secret_number)
    print("Nope, the number I was thinking was " + secret_number + ".")
 

Source Code

import random

print("Hello, please enter your name.")
player_name = input()

secret_number = random.randint(1,100)
print("Thanks, " + player_name + ". Please enter any number between 1 and 100. Remember, you have only 5 attempts")


for total_guesses in range(5):
    guess = int(input())    
    if guess < secret_number:
        print("Your guess is below the number, please guess again")
    elif guess > secret_number:
              print("Your guess is above the number, please guess again")
    else: 
        break

if guess == secret_number:
    total_guesses = str(total_guesses + 1)
    print(player_name + ", it took you " + total_guesses + " attempts to guess the number")

if guess != secret_number:
    secret_number = str(secret_number)
    print("Nope, the number I was thinking was " + secret_number + ".")
 

Posted from Data Science With Python SteemPress : http://datasciencewithpython.info/python-exercise-001-make-a-guess-the-number-game/

Coin Marketplace

STEEM 0.28
TRX 0.13
JST 0.033
BTC 61450.33
ETH 2982.28
USDT 1.00
SBD 3.60