Learn to Code : Number Guessing Game

in #coding4 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.18
TRX 0.16
JST 0.029
BTC 62299.15
ETH 2428.96
USDT 1.00
SBD 2.65