Learn to Code : Number Guessing Game

in #coding5 months ago

num_game.py


import random

def guess_the_number():
    number_to_guess = random.randint(1, 100)
    attempts = 0
    while True:
        guess = int(input("Guess a number between 1 and 100: "))
        attempts += 1
        if guess < number_to_guess:
            print("Too low!")
        elif guess > number_to_guess:
            print("Too high!")   
        else:
            print("Congratulations! You've guessed the number in {attempts} attempts.")
            break

guess_the_number()
Sort:  

Upvoted! Thank you for supporting witness @jswit.

Coin Marketplace

STEEM 0.22
TRX 0.20
JST 0.034
BTC 98504.77
ETH 3362.26
USDT 1.00
SBD 3.06