-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguessing_game_challenge.py
40 lines (27 loc) · 1.05 KB
/
guessing_game_challenge.py
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
import random
attempts_list = []
attempts = 0
rand_number = random.randint(1, 10)
player_name = input("what's your name : ").capitalize()
print (f"Welcome to my game, {player_name} !")
player = input("You wanna play my guessing game ? ").lower()
while player == "yes":
print("I have guessed a number between 1 - 10 ")
while True:
try:
guess = int(input("Enter your guess : "))
attempts += 1
attempts_list.append(attempts)
if (guess < 1 or guess > 10):
raise ValueError("Please guess a number within the given range")
if guess == rand_number:
print(f"Nice!, You got it after {attempts} attempts")
break
elif guess > rand_number :
print("The number is lower ! ")
else:
print("The number is higher ! ")
except ValueError as err:
print(err)
player = input("Do you want to play again? (yes/no) ").lower()
print("Have a nice day ! ")