I recently wrote a program that calculates the hex value when given rgb values. I was just wondering if my code is terrible (i did my best to write it from scratch without much help). I'm still a beginner and trying to learn.
Any help would be greatly appreciated (guidance about how i could do things better etc.).
Thank you
# sets the HEX letters for numbers above 9
hex_table = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8, '9':9,
'a':10, 'b':11, 'c':12, 'd':13, 'e':14, 'f':15}
# creates variable for the keys in dictionary
key_list = list(hex_table.keys())
# creates variable for values in dictionary
val_list = list(hex_table.values())
def test(r= int(input('red value: ')),g= int(input('green value: ')), b= int(input('blue value: '))):
# finds the index of the value
red_value = r // 16
green_value = g // 16
blue_value = b // 16
# Calcuate the remainder
red_float = float(r) / 16
red_remainder = red_float % 1
green_float = float(g) / 16
green_remainder = green_float % 1
blue_float = float(b) / 16
blue_remainder = blue_float % 1
# adds '#' in front of the result
print('#',end='')
#find the first two values in HEX code
if r >= 10:
print(key_list[val_list.index(red_value)],end='')
second_letter = (int(red_remainder * 16))
print(key_list[val_list.index(second_letter)],end='')
elif r <10:
print(red_value,end='')
print(int(red_remainder * 16),end='')
#find the next two values
if g >= 10:
print(key_list[val_list.index(green_value)],end='')
second_letter = (int(green_remainder * 16))
print(key_list[val_list.index(second_letter)],end='')
elif g <10:
print(green_value,end='')
print(int(green_remainder * 16),end='')
#find the last two values
if b >= 10:
print(key_list[val_list.index(blue_value)],end='')
second_letter = (int(blue_remainder * 16))
print(key_list[val_list.index(second_letter)],end='')
elif b <10:
print(blue_value,end='')
print(int(blue_remainder * 16),end='')
test()