Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added a Python script for Number Guessing Game #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Saurabh6266 wants to merge 1 commit into DhanushNehru:main
base: main
Choose a base branch
Loading
from Saurabh6266:add-number-guessing-game
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions number_guessing_game.py
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Program that implements a number guessing game.

import random

def get_level():
while True:
try:
n = int(input("Level: ").strip())
if n > 0:
return n
except ValueError:
pass

def get_guess():
while True:
try:
guess = int(input("Guess: ").strip())
if guess > 0:
return guess
except ValueError:
pass

def guessing_game():
print("Welcome to the Guessing Game!")
n = get_level()
number_to_guess = random.randint(1, n)
while True:
user_guess = get_guess()
if user_guess > number_to_guess:
print("Too large!")
elif user_guess < number_to_guess:
print("Too small!")
else:
print("Just right!")
break
print("Thanks for playing!")

if __name__ == "__main__":
guessing_game()

AltStyle によって変換されたページ (->オリジナル) /