forked from reemanaqvi/HW04
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HW04_ex00.py
71 lines (55 loc) · 1.92 KB
/
HW04_ex00.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
68
69
70
71
#!/usr/bin/env python
# HW04_ex00
# Create a program that does the following:
# - creates a random integer from 1 - 25
# - asks the user to guess what the number is
# - validates input is a number
# - tells the user if they guess correctly
# - if not: tells them too high/low
# - only lets the user guess five times
# - then ends the program
################################################################################
# Imports
from random import randint
import sys
# Body
def get():
user = raw_input("\n\nEnter your guess number : ")
return user
def match(user,actual):
if (user == actual):
print " congrats! you guessed it correctly!"
return True
else:
if (user < actual):
print "\nToo Low! " + " Please try again! "
else:
print "\nToo high " + " Please try again! "
return False
################################################################################
def main():
count = 0
actual = (randint(1,25))
while (count<5):
user = get()
try:
user_int = int(user)
except:
print "\nThats not a number. Please try again"
count = count + 1
continue
else:
if ((user_int < 1) or (user_int > 25)):
print "\nPlease enter a number in the range of 1-25. Try Again"
count = count + 1
continue
else:
result = match(user_int, actual)
if (result == True):
sys.exit()
count = count + 1
# print "\nActual Number was : " + str(actual)
print "\nSo Actual Number was : " + str(actual) + "!!"
print "\nSorry, you have exhausted your number of trials now"
if __name__ == '__main__':
main()