-
Notifications
You must be signed in to change notification settings - Fork 0
/
numberGuess.py
67 lines (58 loc) · 2.26 KB
/
numberGuess.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
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
What’s the number?
Concepts needed: print, while loop, if/else statements, random function, input/ output
This is a guessing game that the user plays with the program/computer.
The program generates a random number using the random function. The user tries to guess it by inputting a value.
The program then indicates whether the guess is right or not. If it is wrong, then the program should tell how off the guess was.
If it is right, there should be another positive indicator. You can place a limit on the number of guesses allowed.
You will also need functions to compare the inputted number with the guessed number, to compute the difference between the two,
and to check whether an actual number was inputted or not.
"""
import random
score = 0
gamesPlayed = 0
def inputnum():
flag = True
while flag:
guess = input("Try to guess the number generated by computer: ")
if guess.isdigit():
flag = False
return int(guess)
def game():
# generating random number, different from randint as this does not include the end point
print("Computer has generated a random number between 1 and 10. Get it right in 5 tries!")
num = random.randrange( 1, 11)
# Asking user for a guess
tries = 5
global score
global gamesPlayed
gamesPlayed += 1
while(tries!=0):
tries -= 1
guess = inputnum()
print(guess == num)
if(guess == num):
print("Congratulations!!")
score = score + 1
playAgain()
break
elif(tries == 0):
print("You are out of tries! Play again (Y/N)?")
response = input()
playAgain()
break
else:
if(guess < num):
print("The guess is less than the number")
else:
print("The guess is more than the num")
def playAgain():
response = input("Wanna play again? Type Y/N?\n")
if(response == 'Y'):
game()
else:
print("Thank You for playing. Your score: ", score , "/" , gamesPlayed)
if __name__ == '__main__':
print("***Welcome to the Game Expo!***\n")
print("We have got a guessing game for you today\n")
game()