Question: Ii used this code to get the expected output: def print_menu(): print(Base codes:) print( dec - Decimal) print( bin - Binary) print( oct - Octal)
Ii used this code to get the expected output:
def print_menu(): print("Base codes:") print(" dec - Decimal") print(" bin - Binary")
print(" oct - Octal") print(" hex - Hexadecimal") print() print("Enter a value, base code to switch base, h to redisplay the base codes or q to quit.") def check_match(input_string,available,base): flag = True for i in input_string: if i not in available: flag = i break if flag != True: print("Invalid character '{0}' in string {1} for base {2}".format(flag,input_string,base)) else: return True def convert(input_string,base): int_val = None binary = ['0','1'] decimal = [str(i) for i in range(0,10)] octal = [str(i) for i in range(0,8)] hexadecimal = decimal + ['A','B','C','D','E','F'] if base == 10: valid = check_match(input_string,decimal,base) if valid: int_val = int(input_string) elif base == 2: valid = check_match(input_string,binary,base) if valid: int_val = int(input_string,2) elif base == 8: valid = check_match(input_string,octal,base) if valid: int_val = int(input_string,8) elif base == 16: valid = check_match(input_string,hexadecimal,base) if valid: int_val = int(input_string,16) if int_val: print("{0} {1}b o{2} 0x{3}".format(int_val,bin(int_val)[2:],oct(int_val)[2:],(hex(int_val)[2:].upper()))) if __name__ == '__main__': print("Base Numeric Converter 1.0") print() print_menu() prompt = "dec> " base = 10 baseindex = {'dec':10,'bin':2,'oct':8,'hex':16} while True: input_string = input(prompt) if input_string == "q": break elif input_string in ['dec','bin','oct','hex']: prompt = input_string + "> " base = baseindex[input_string] elif input_string == "h": print_menu() else: convert(input_string,base)
how can I fix it to get the expected output?
Base Numeric Converter 1.0 Base codes: dec - Decimal bin - Binary oct - Octal hex - Hexadecimal Your output Enter a value, base code to switch base, h to redisplay the base codes a dec> 13 1101b 015 OxD dec> 122 1111010b o172 Ox7A dec> bin> 6 110b Ox6 bin> 45 101101b 055 Ox2D bin> oct> 59 111011b 073 0x3B oct> 127 1111111b o177 Ox7F oct> hex> Invalid character 'a' in string a9 for base 16 hex> Invalid character 'f' in string ff for base 16 hex> Base codes: dec - Decimal bin - Binary oct - Octal hex - Hexadecimal Enter a value, base code to switch base, h to redisplay the base codes a hex> Invalid character 'k' in string kk for base 16 hex> bin> Invalid character '2' in string 100201 for base 2 bin> Base Numeric Converter 1.0 Base codes: dec - Decimal bin - Binary oct - Octal hex - Hexadecimal Expected output Enter a value, base code to switch base, h to redisplay the base codes o dec> 13 1101b 015 OxD dec> 122 1111010b o172 Ox7A dec> bin> 6 110b Ox6 bin> 45 101101b 055 0x2D bin> oct> 59 111011b 073 Ox3B oct> 127 1111111b 0177 Ox7F oct> hex> 169 10101001b 0251 OxA9 hex> 255 11111111b 0377 OxFF hex> Base codes: dec - Decimal bin - Binary oct - Octal hex - Hexadecimal Enter a value, base code to switch base, h to redisplay the base codes a hex> Invalid character 'k' in string kk for base 16 hex> bin> Invalid character '2' in string 100201 for base 2 bin>
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
