forked from CodeSadhu/Hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guess_the_number.py
47 lines (46 loc) · 1.7 KB
/
guess_the_number.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
import random
print("*****GUESS THE NUMBER*****")
attemptsList= []
def score():
if len(attemptsList) <= 0:
print("There is currently no high score, it's yours for the taking!")
else:
print("High score is {} attempts".format(min(attemptsList)))
def startGame():
randomNumber = int(random.randint(1, 10))
print("Welcome buddy!")
playerName = input("Your name? ")
play = input("Hi, {}!! Let's start? (Enter Yes/No) ".format(playerName))
attempts = 0
score()
print("Happy Guessing:)")
while play.lower() == "yes":
try:
guess = input("Pick a number between 1 and 10 ")
if int(guess) < 1 or int(guess) > 10:
raise ValueError("Guess within the range, ok?")
if int(guess) == randomNumber:
print("Kudos!! You Won!")
attempts += 1
attemptsList.append(attempts)
print("It took you {} attempts".format(attempts))
playAgain = input("Would you like to play again? (Enter Yes/No) ")
attempts = 0
score()
randomNumber = int(random.randint(1, 10))
if playAgain.lower() == "no":
print("That's cool, have a good one!")
break
elif int(guess) > randomNumber:
print("It's lower")
attempts += 1
elif int(guess) < randomNumber:
print("It's higher")
attempts += 1
except ValueError as err:
print("Oh no! Try again...")
print("({})".format(err))
else:
print("That's cool, have a good one!")
if __name__ == '__main__':
startGame()