forked from fenyx-it-academy/Class4-PythonModule-Week4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathW4_Q3_Guess_the_number.py
More file actions
67 lines (56 loc) · 2.07 KB
/
W4_Q3_Guess_the_number.py
File metadata and controls
67 lines (56 loc) · 2.07 KB
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
#C@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@1
# PyCoder Coding Class:4 @
# Cabir Erguven @
# Week : 4 @
# Question : 3 @
# Date: 30.01.2021 @
#C@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@1
#
# Number Guessing Game
#
# Guess a number the computer chooses in the range I chose.
# So that I can try to find the correct number which was selected by computer.
#
# Acceptance Criteria:
#
# Computer must randomly pick an integer from user selected a range,
# i.e., from A to B, where A and B belong to Integer.
# Your program should prompt the user for guesses
# if the user guesses incorrectly, it should print whether the guess is too high or too low.
# If the user guesses correctly, the program should print total time and total number of guesses.
# You must import some required modules or packages
# You can assume that the user will enter valid input.
from timeit import default_timer as timer
import random
print("******* GUESS THE NUMBER *******")
print("Enter the range of the number you want to find")
while True:
try:
begin = int(input("Enter the first number: "))
finish = int(input("Enter the last number: "))
break
except ValueError:
print("Please enter only number")
start = timer()
number = random.randint(begin, finish)
count = 0
guess = []
while guess != number:
count += 1
# try:
# guess = int(input("Please enter a number: "))
# except ValueError:
# print("Please enter a valid number!")
try:
guess = int(input("\nEnter your guess: "))
if guess > number:
print(" It is too high")
elif guess < number:
print(" It is too low")
else:
print("\nCongratulation!!!")
print("You guessed in",count,"times.")
except (ValueError, TypeError):
print("Please enter a valid number")
end = timer()
print("It took", round((end - start), 2), "seconds to finish the game!")