forked from whaleygeek/MyLittleComputer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecimal.py
64 lines (50 loc) · 1.71 KB
/
decimal.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
# decimal.py 21/08/2015 D.J.Whale
#
# Read and write decimal 3 digit unsigned numbers
# with zero padding
DEFAULT_WIDTH = 3
def read(width=None, file=None):
#print("read")
"""return a decimal number in range 000-999"""
# default width is 3 characters, but you can ask for wider
if width == None:
width = DEFAULT_WIDTH
if file == None: # stdin, strip blank lines
#print("stdin")
while True:
try:
try:
line = raw_input()
except:
line = input()
except EOFError:
#print(" EOF")
return None # EOF
line = line.strip() # strip wrapping spaces and newline char
if len(line) != 0:
instr = int(line)
#print(" instr:" + str(instr))
return instr
else: # from file, strip blank lines
#print("file")
#raise RuntimeError("HERE")
while True:
line = file.readline()
if line == "":
#print(" EOF")
return None # EOF
line = line.strip() # strip wrapping spaces and newline char
if len(line) != 0:
instr = int(line)
#print(" instr:" + str(instr))
return instr
def write(number, width=None, file=None):
#print("write: %s %s %s %s" %( str(number) , str(type(number)), str(width), str(type(width))))
"""write a decimal number 000-999 zero padded"""
if width == None:
width = DEFAULT_WIDTH
if file == None: # stdout
print(str(number).zfill(width))
else: # to file
file.write(str(number).zfill(width) + "\n")
# END