-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber_guessing.py
More file actions
41 lines (24 loc) · 930 Bytes
/
number_guessing.py
File metadata and controls
41 lines (24 loc) · 930 Bytes
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
import random
def number_guessing_game(attempts_limit=7):
number_to_guess = random.randint(1, 100)
guessed_correctly = False
attempts = 0
print("Welcome to Number guessing game")
print("I have selected a number from 1-100, can you guess it?")
while attempts < attempts_limit and not guessed_correctly:
try:
guess = int(input("Please add your guess: "))
attempts +=1
if guess < number_to_guess:
print("Too low!")
elif guess > number_to_guess:
print("Too high")
else:
guessed_correctly = True
print(f"Congratulaitions, you guessed the number in {attempts} attempts")
except ValueError:
print("Oops! This is not a valid number, please a whole number")
if not guessed_correctly:
print(f"You are out of guesses, the correct guess was {number_to_guess}")
print("Game over, Thanks for playing!")
number_guessing_game()