forked from reemanaqvi/HW04
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW04_ex00.py
110 lines (78 loc) · 2.31 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/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
import textToNumber
import random
import sys
# Body
def create_random_int():
return random.randint (1,25)
def compare(a,b):
if(a == b):
return "True"
elif(a > b):
return "too high"
else :
return "too low"
def take_input(randomNum):
numbr = raw_input("Enter a number :")
# print ("Random number is : "+str(randomNum))
validate(randomNum, numbr)
def validate_for_int_from_text(randomNum, numbr):
try:
int_number_from_Text = textToNumber.text2num(numbr.lower())
if (1 <= int_number_from_Text and int_number_from_Text <= 25):
compareResult = compare(int_number_from_Text,randomNum)
if(compareResult == "True"):
print ("Congratulations! You did hit the mark my friend")
sys.exit()
else :
print ("Better luck next time")
print ("Your guess was "+compareResult)
else :
raise ArithmeticError
except ArithmeticError :
print ("Number entered is not within the limits. Please enter something within 1 to 25")
print
except Exception as e:
print ("Invalid input")
print e
def validate(randomNum, numbr):
try:
int_number = int (numbr)
except Exception:
validate_for_int_from_text(randomNum, numbr)
else:
try :
if (1 <= int_number and int_number <= 25):
compareResult = compare(int_number,randomNum)
if(compareResult == "True"):
print ("Congratulations! You did hit the mark my friend")
print
sys.exit()
else :
print ("Better luck next time")
print ("Your guess was "+compareResult)
else :
raise ArithmeticError
except ArithmeticError :
print ("Number entered is not within the limits. Please enter something within 1 to 25")
print
################################################################################
def main():
randomNum = create_random_int()
i = 0
while (i<5):
take_input(randomNum)
i += 1
if __name__ == '__main__':
main()