Skip to content

Commit a2cb6f8

Browse files
authored
Create guess_the_number.py
1 parent c0108ac commit a2cb6f8

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# import random library to use it in this program.
2+
import random
3+
4+
# Variable num generates and stores a random number between 0 to 100.
5+
num = random.randrange(0, 100)
6+
guessCheck = "wrong"
7+
print("--Welcome to Guess The Number Game--")
8+
9+
# Iterate using while loop
10+
while guessCheck == "wrong":
11+
res = int(input("Please input a number between 0 and 100:"))
12+
try:
13+
val = int(res)
14+
except ValueError:
15+
print("This is not a valid integer. Please try again!")
16+
continue
17+
# Check whether the user input is high or low than the random number generated.
18+
if val < num:
19+
print("This is lower than actual number. Please try again!")
20+
elif val > num:
21+
print("This is higher than actual number. Please try again!")
22+
else:
23+
# Final result will be printed with this message as the user input matches the random number generated
24+
print("Hurray! you won, this is the correct number.")
25+
guessCheck = "correct"
26+
print("Thank you for playing Guess The Number. See you again!")

0 commit comments

Comments
 (0)