Skip to content

Commit 26135bd

Browse files
Create Guess the number.py
1 parent a0e00d2 commit 26135bd

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

Guess the number.py

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#Author : Vishwajeet Bamane
2+
3+
#Last modified Date:10/05/2020
4+
5+
# Number Guessing Game
6+
7+
# using Python
8+
9+
#importing random module to generate random number of chances
10+
11+
import random
12+
13+
print("Number guessing game")
14+
15+
#generating random rumber using randint() between 0 and 9
16+
17+
number = random.randint(0,9)
18+
19+
chances = 0
20+
21+
22+
23+
print("Guess a number (between 1 and 9 ):")
24+
25+
26+
27+
# While loop to count the number
28+
29+
# of chances
30+
31+
while chances < 5:
32+
33+
34+
35+
# Enter a number between 1 to 9
36+
37+
guess = int(input())
38+
39+
40+
41+
# Compare the user entered number
42+
43+
# with the number to be guessed
44+
45+
if guess == number:
46+
47+
48+
49+
# if number entered by user
50+
51+
# is same as the generated
52+
53+
# number by randint function then
54+
55+
# break from loop using loop
56+
57+
# control statement "break"
58+
59+
print("Congratulation YOU WON!!!")
60+
61+
break
62+
63+
64+
65+
# Check if the user entered
66+
67+
# number is smaller than
68+
69+
# the generated number
70+
71+
elif guess < number:
72+
73+
print("Your guess was too low: Guess a number higher than", guess)
74+
75+
76+
77+
# The user entered number is
78+
79+
# greater than the generated
80+
81+
# number
82+
83+
else:
84+
85+
print("Your guess was too high: Guess a number lower than", guess)
86+
87+
88+
89+
# Increase the value of chance by 1
90+
91+
chances +=1
92+
93+
94+
95+
96+
97+
# Check whether the user
98+
99+
# guessed the correct number
100+
101+
if chances >=5:
102+
103+
print("YOU LOSE!!! The number is", number)

0 commit comments

Comments
 (0)