This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def menu(): | |
| ''' | |
| function holding the menu | |
| Returns: string | |
| ''' | |
| return ( | |
| ''' | |
| ---------------------------------------------------------- | |
| Welcome to the Number Base converter! | |
| The system will ask you for: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def menu(): | |
| ''' | |
| function holding the menu | |
| Returns: string | |
| ''' | |
| return ( | |
| ''' | |
| ---------------------------------------------------------- | |
| Welcome to the Number Base converter! | |
| The system will ask you for: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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))] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 <<<') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
OlderNewer