-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
54 lines (42 loc) · 1.39 KB
/
main.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import random
print("Number guessing game")
# randint function to generate the
# random number b/w 1 to 9
number = random.randint(1, 9)
# number of chances to be given
# to the user to guess the number
# or 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 loop to count the number
# of chances
while True:
# 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(
f'CONGRATULATIONS! YOU HAVE GUESSED THE \
NUMBER {number} IN {chances} ATTEMPTS!')
# Printing final statement using the f-strings method;
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)
# Increase the value of chance by 1
chances += 1