Skip to content

Commit 90eda76

Browse files
PrashantPrashant
Prashant
authored and
Prashant
committed
Made Guessing game using purely Functions to make program more robust
1 parent 1f8cd47 commit 90eda76

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

HighLowGame_new.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from random import randint
2+
3+
logo = '''
4+
/ _ \_ _ ___ ___ ___ /__ \ |__ ___ /\ \ \_ _ _ __ ___ | |__ ___ _ __
5+
/ /_\/ | | |/ _ \/ __/ __| / /\/ '_ \ / _ \ / \/ / | | | '_ ` _ \| '_ \ / _ \ '__|
6+
/ /_\\| |_| | __/\__ \__ \ / / | | | | __/ / /\ /| |_| | | | | | | |_) | __/ |
7+
\____/ \__,_|\___||___/___/ \/ |_| |_|\___| \_\ \/ \__,_|_| |_| |_|_.__/ \___|_|
8+
'''
9+
10+
EASY_LEVEL_TURNS = 10
11+
HARD_LEVEL_TURNS = 5
12+
13+
#Function to check user's guess against actual answer.
14+
def check_answer(guess, answer, turns):
15+
"""checks answer against guess. Returns the number of turns remaining."""
16+
if guess > answer:
17+
print("Too high.")
18+
return turns - 1
19+
elif guess < answer:
20+
print("Too low.")
21+
return turns - 1
22+
else:
23+
print(f"You got it! The answer was {answer}.")
24+
25+
#Make function to set difficulty.
26+
def set_difficulty():
27+
level = input("Choose a difficulty. Type 'easy' or 'hard': ")
28+
if level == "easy":
29+
return EASY_LEVEL_TURNS
30+
else:
31+
return HARD_LEVEL_TURNS
32+
33+
def game():
34+
print(logo)
35+
#Choosing a random number between 1 and 100.
36+
print("Welcome to the Number Guessing Game!")
37+
print("I'm thinking of a number between 1 and 100.")
38+
answer = randint(1, 100)
39+
print(f"Pssst, the correct answer is {answer}")
40+
41+
turns = set_difficulty()
42+
#Repeat the guessing functionality if they get it wrong.
43+
guess = 0
44+
while guess != answer:
45+
print(f"You have {turns} attempts remaining to guess the number.")
46+
47+
#Let the user guess a number.
48+
guess = int(input("Make a guess: "))
49+
50+
#Track the number of turns and reduce by 1 if they get it wrong.
51+
turns = check_answer(guess, answer, turns)
52+
if turns == 0:
53+
print("You've run out of guesses, you lose.")
54+
return
55+
elif guess != answer:
56+
print("Guess again.")
57+
58+
59+
game()

0 commit comments

Comments
 (0)