-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
56 lines (43 loc) · 1.43 KB
/
main.py
File metadata and controls
56 lines (43 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from art import logo
import random
from replit import clear
print("Welcome to Number Guessing Game!")
hard_level = 5
easy_level = 10
def set_difficulty():
"""This function returns the number of attempts """
level = input("Choose your difficulty level 'easy or hard? ")
if level == "easy":
return easy_level
else:
return hard_level
def check_answer(guess, answer, turns):
"""This function takes the generated answer, user's guess and reduce the number of turns """
if guess > answer:
print("Too high")
return turns -1
elif guess < answer:
print("Too low")
return turns -1
else:
print(f"Wow, you got it! The answer is indeed {answer} and your remaining attempts are {turns}")
def game():
auto_answer = random.randint(1, 100)
print(logo)
print(auto_answer)
turns = set_difficulty()
#Repeat the guessing functionality if they get it wrong.
user_guess = 0
while user_guess != auto_answer:
print(f"You have {turns} attempts remaining to guess the number.")
user_guess = int(input("Make a guess?"))
#Track the number of turns and reduce by 1 if they get it wrong.
turns = check_answer(user_guess,auto_answer,turns)
if turns == 0:
print("You have ran out of Guesses, play again!")
return
elif user_guess != auto_answer:
print("Guess again")
while input("\n Would you like to play a Guessing Game? 'Yes' or 'No' to exit.\n").lower() == "yes":
clear()
game()