-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBTCInput2.py
177 lines (162 loc) · 6 KB
/
BTCInput2.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
DEBUG_MODE = True
from dateutil.parser import parse
#create read_date function
def read_text(prompt):
'''
Displays a prompt and reads in a string of text.
Keyboard interrupts (CTRL+C) are ignored
returns a string containing the string input by the user
'''
while True: # repeat forever
try:
result=input(prompt) # read the input
# if we get here no exception was raised
if result=='':
#don't accept empty lines
print('Please enter text')
else:
# break out of the loop
break
except KeyboardInterrupt:
# if we get here the user pressed CTRL+C
print('Please enter text')
if DEBUG_MODE:
raise Exception('Keyboard interrupt')
# return the result
return result
#def read_bool(prompt='y or n', yes='y', no='n', yes_option='confirm', no_option='cancel'):
# choice_string = ' '+ '{0} to {1}, {2} to {3} '.format(
# yes, yes_option, no, no_option)
# #The choice_string formats the choices that the user has to make when
# #making a yes or no decision
# while True:
# choice = read_text(prompt=prompt+choice_string+' ')
# if choice == yes:
# return True
# elif choice == no:
# return False
# else:
# print('Please enter {0} to {1}, {1} to {2}')
def read_bool(decision='(y or n)', yes='y', no='n', yes_option='confirm', no_option='cancel'):
choice_string = '{0} {1} to {2}, {3} to {4}: '.format(decision,
yes, yes_option, no, no_option)
#The choice_string formats the choices that the user has to make when
#making a yes or no decision
while True:
choice = read_text(prompt=choice_string)
if choice == yes:
return True
elif choice == no:
return False
else:
print('Invalid Entry')
# print('Please enter {0} to {1}, {2} to {3}'.format(yes_option, yes, no_option, no))
def read_date(prompt):
'''
Displays a prompt and reads in a date. Keyboard interrupts (CTRL+C) are
ignored. Invalid dates are rejected. Returns a datetime.date object
containing the value input by the user
'''
while True:
try:
date_text = read_text(prompt)
#if date_text == '':
# print('please enter text')
result = parse(date_text)
result = result.date()
break
#pass
except ValueError:
print('Please enter a valid date')
#pass
return result
def read_number(prompt,function):
'''
Displays a prompt and reads in a floating point number.
Keyboard interrupts (CTRL+C) are ignored
Invalid numbers are rejected
returns a float containing the value input by the user
'''
while True: # repeat forever
try:
number_text=read_text(prompt)
result=function(number_text) # read the input
# if we get here no exception was raised
# break out of the loop
break
except ValueError:
# if we get here the user entered an invalid number
print('Please enter a number')
# return the result
return result
def read_number_ranged(prompt, function, min_value, max_value):
'''
Displays a prompt and reads in a number.
min_value gives the inclusive minimum value
max_value gives the inclusive maximum value
Raises an exception if max and min are the wrong way round
Keyboard interrupts (CTRL+C) are ignored
Invalid numbers are rejected
returns a number containing the value input by the user
'''
if min_value>max_value:
# If we get here the min and the max
# are wrong way round
raise Exception('Min value is greater than max value')
while True: # repeat forever
result=read_number(prompt,function)
if result<min_value:
# Value entered is too low
print('That number is too low')
print('Minimum value is:',min_value)
# Repeat the number reading loop
continue
if result>max_value:
# Value entered is too high
print('That number is too high')
print('Maximum value is:',max_value)
# Repeat the number reading loop
continue
# If we get here the number is valid
# break out of the loop
break
# return the result
return result
def read_float(prompt):
'''
Displays a prompt and reads in a floating point number.
Keyboard interrupts (CTRL+C) are ignored
Invalid numbers are rejected
returns a float containing the value input by the user
'''
return read_number(prompt,float)
def read_int(prompt):
'''
Displays a prompt and reads in an integer number.
Keyboard interrupts (CTRL+C) are ignored
Invalid numbers are rejected
returns an int containing the value input by the user
'''
return read_number(prompt,int)
def read_float_ranged(prompt, min_value, max_value):
'''
Displays a prompt and reads in a floating point number.
min_value gives the inclusive minimum value
max_value gives the inclusive maximum value
Raises an exception if max and min are the wrong way round
Keyboard interrupts (CTRL+C) are ignored
Invalid numbers are rejected
returns a number containing the value input by the user
'''
return read_number_ranged(prompt,float,min_value,max_value)
def read_int_ranged(prompt, min_value, max_value):
'''
Displays a prompt and reads in an integer point number.
min_value gives the inclusive minimum value
max_value gives the inclusive maximum value
Raises an exception if max and min are the wrong way round
Keyboard interrupts (CTRL+C) are ignored
Invalid numbers are rejected
returns a number containing the value input by the user
'''
return read_number_ranged(prompt,int,min_value,max_value)