-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseInput.py
149 lines (127 loc) · 4.4 KB
/
ParseInput.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
from ConvertToDec import convert_to_dec
from ConvertToBin import convert_to_bin
from ConvertToHex import convert_to_hex
'''
get_input is a function that takes a string text
and returns the user input, unless the user
input 'EXIT' which makes the program exit.
'''
def get_input (text):
i = input (text)
if i == "EXIT":
print ("PROCESS IS IMMEDIATELY EXITING - THE NEW CONVERSIONS WON'T BE SAVED")
quit ()
return i
'''
wait_for_input is a function that is made to get user's input,
in a more bug-robust way. It basically prints a text,
gets user's input, and if the user's input is in one of
the possible supported inputs, then it is returned.
Otherwise, the function prints an error text and gets user's input
until the input is a supported one.
This function takes 2 strings (text, error_text) and a variadic number
of following arguments (*possible_inputs) corresponding to
the different available inputs that the user can choose.
'''
def wait_for_input (text, error_text, *possible_inputs):
current_input = get_input (text)
while current_input not in possible_inputs:
print (error_text)
current_input = get_input (text)
return current_input
'''
is_binary is a function that checks if a string is a binary number.
Since binary notation only uses zeros and ones, the sum of the number of "0"
and "1" in a string should be equal to its length, in case the string really
is a binary number.
is_binary takes a string n, and returns a bool
'''
def is_binary (n):
return len (n) == (n.count ("0") + n.count ("1"))
'''
is_decimal is a function that checks if a string is
a decimal number by checking if every character
is a digit.
is_decimal takes a string n and returns a bool.
'''
def is_decimal (n):
for char in n:
if char not in "0123456789":
return False
return True
'''
is_hexadecimal is a function that checks if a string is a hexadecimal number,
by checking if every character is in
"0123456789abcdef" (the hexadecimal different coefficients).
is_hexadecimal takes a string n and returns a bool
'''
def is_hexadecimal (n):
for char in n:
if char.lower () not in "0123456789abcdef":
return False
return True
'''
is_input_valid is a function that checks if the user's input
fits its base.
is_input_valid takes two strings : n and initial_base,
and returns a bool.
'''
def is_input_valid (n, initial_base):
if initial_base == "dec":
return is_decimal (n)
elif initial_base == "hex":
return is_hexadecimal (n)
elif initial_base == "bin":
return is_binary (n)
else:
return False
'''
get_n_and_base is a function that gets user's input number
and its base.
It takes no arguments, and returns two strings: n and initial_base
'''
def get_n_and_base ():
while True:
n = get_input ("Insert the number : ")
while n == "":
print ("ERROR, NUMBER ISN'T INPUT CORRECTLY!!")
n = get_input ("Insert the number again : ")
initial_base = wait_for_input (
"Insert the number's base (dec/bin/hex) : ",
"ERROR, INITIAL BASE IS NOT SUPPORTED YET OR IS INVALID",
"dec", "bin", "hex",
)
if is_input_valid (n, initial_base):
break
else:
print ("ERROR, THE INPUT NUMBER DOESN'T FIT ITS BASE OR IS INVALID")
return n, initial_base
'''
get_inputs is a function that gets all necessary user's inputs.
It takes no arguments, and returns three strings :
n, initial_base and final_base
'''
def get_inputs ():
n, initial_base = get_n_and_base ()
final_base = wait_for_input (
"To which base do you want to conver this number? (dec/bin/hex) : ",
"ERROR, FINAL BASE IS NOT SUPPORTED YET OR IS INVALID",
"dec", "bin", "hex"
)
return n, initial_base, final_base
'''
output_result is a function that takes 3 arguments
(string n, initial_base, final_base) and outputs
the result of the number base conversion.
It returns the result of the conversion.
'''
def output_result (n, initial_base, final_base):
final_n = ""
if final_base == "hex":
final_n = convert_to_hex (n, initial_base)
elif final_base == "dec":
final_n = convert_to_dec (n, initial_base)
elif final_base == "bin":
final_n = convert_to_bin (n, initial_base)
print ("The number %s (%s) in %s base is %s" % (n, initial_base, final_base, final_n))
return final_n