-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber guessing game.py
40 lines (31 loc) · 1.34 KB
/
number guessing game.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
# Number Guessing Game implementation
# using Python
import random
print("Number guessing game")
# randint function to generate the number between 1 to 9
number = random.randint(1, 9)
# number of chances to be given to the user to guess the number
# it is the inputs given by user into input box here number of
# chances are 5
chances = 0
print("Guess a number (between 1 and 9):")
while chances < 5:
# Enter a number between 1 to 9
guess = int(input())
# Compare the user entered number with the number to be guessed
if guess == number:
# if number entered by user is same as the generated
# number by randint function then break from loop using loop
# control statement "break"
print("Congratulation YOU WON!!!")
break
# Check if the user entered number is smaller than the generated number
elif guess < number:
print("Your guess was too low: Guess a number higher than", guess)
# The user entered number is greater than the generated number
else:
print("Your guess was too high: Guess a number lower than", guess)
chances += 1
# other you will get the below message
if not chances < 5:
print("YOU LOSE!!! The number is", number)