- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecked_input.py
More file actions
Latest commit
60 lines (51 loc) · 1.88 KB
/
Copy pathchecked_input.py
File metadata and controls
60 lines (51 loc) · 1.88 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
fromenumimportEnum,auto
classInput:
def__init__(self,prompt):
self.prompt=prompt+" "
self.ready=False
self.value=None
defrun(self):
self.value=input(self.prompt)
self.ready=True
classNumberType(Enum):
FLOAT=auto()
INTEGER=auto()
classNumberInput(Input):
def__init__(self,prompt,num_type=NumberType.FLOAT):
self.type=num_type
Input.__init__(self,prompt+" ")
defrun(self):
self.value=input(self.prompt)
input_is_num=False
while(notinput_is_num):
try:
ifself.type==NumberType.FLOAT:
self.value=float(self.value)
else:
self.value=int(self.value)
input_is_num=True
except:
ifself.type==NumberType.FLOAT:
print("Your response could not be converted into a floating point number. Please try again!")
else:
print("Your response could not be converted into a integer number. Please try again!")
self.ready=True
classFlagInput(Input):
def__init__(self,prompt,flags):
self.flags= []
forflaginflags:
self.flags.append(flag)
Input.__init__(self,prompt+" ")
defrun(self):
is_flag=False
whilenotis_flag:
response=input(self.prompt)
ifresponseinself.flags:
is_flag=True
self.value=response
self.ready=True
else:
print("That is not a valid response--please try one of the following flags: ",end="")
fornuminrange(len(self.flags)-1):
print(self.flags[num],end=" ")
print(self.flags[len(self.flags)-1])