Skip to content

Instantly share code, notes, and snippets.

View martinandersson82's full-sized avatar

martinandersson82

View GitHub Profile
def menu():
'''
function holding the menu
Returns: string
'''
return (
'''
----------------------------------------------------------
Welcome to the Number Base converter!
The system will ask you for:
def menu():
'''
function holding the menu
Returns: string
'''
return (
'''
----------------------------------------------------------
Welcome to the Number Base converter!
The system will ask you for:
def validate_bin(check_number):
'''
function that checks if the input consists of either 0 or 1.
Binary numbers can only be 0 or 1, so we need to validate this
before doing any math on it.
returns: True/False
'''
check_list = [int(item) for item in set(list(check_number))]
def validate_input(input_number):
'''
function that checks if the input consists of legal characters.
Binary numbers can only be 0 or 1, so we need to validate this
before doing any math on it.
returns: True/False
'''
legal_char = '0123456789abcdef'
for number in input_number:
def validator(input_number,input_base,output_base):
if validate_input(input_number) and input_base.isdigit() and output_base.isdigit():
if int(input_base) == 2:
if not input_number.isdigit():
print ('>>> ERROR: Input is not a digit. Binary Numbers contain 0s and 1s only. <<<')
return False
if not validate_bin(input_number):
print ('>>> ERROR: Invalid Binary Number. Must contain 0s or 1s <<<')
def convert_number_system(input_number, input_base, output_base):
'''
function that calculates numbers from one base to the other
returns: converted number
'''
#list that holds the numbers to output in the end
remainder_list = []
# if the user asked for a Hexadesimal output, we need to convert
# any number from 10 and up.
if int(output_base) == 16:
hex_dict = {'10' : 'a' , '11' : 'b' , '12' : 'c' , '13' : 'd' , '14' : 'e' , '15' : 'f'}
#loop through remainder_list and convert 10+ to letters.
for index, each in enumerate(remainder_list):
for key, value in hex_dict.items():
if each == key:
remainder_list[index] = value
def execute_converter():
'''
procedure that interacts with the user and sends it to get converted.
'''
#proceed set to y/yes to initiate the while loop.
proceed = 'y'
while proceed.lower() == 'y':
valid_input = False
my_list = ['a' , 'b' , 'n' , 'x' , 1 , 2 , 3 , 'a' , 33.3 , 'n' , 'b']
number_list = []
string_list = []
for item in my_list:
print (f'current item: {item}, Type: {type(item)}')
if not isinstance(item,str):
number_list.append(item)
else:
string_list.append(item)
my_list = string_list
def get_numbers(input_char):
if not isinstance(input_char,str):
return True
return False
my_list = [1,2,3,'a','b','c']
check_list = filter(get_numbers, my_list)
for items in check_list:
print(items)