Number Guessing Game : Learn to Code

in #hive-1882625 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:  

Source of potential text plagiarism

Hello.

Plagiarism is the copying & pasting of others' work without giving credit to the original author or artist.
We would appreciate it if you could avoid plagiarism of content (full or partial texts, videos, photography, art, etc.).

Thank you.

Guide: Why and How People Abuse and Plagiarise

If you believe this comment is in error, please contact us in #appeals in Discord.

Code is code. It only works one way.

Curated by @arc7icwolf.byte for the #LearnToCode community.