Is there a way to shorten this code challenge using python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Is there a way to shorten this code challenge using python

It's my second day coding and the code looks very long, and want to see how professionals like you will write it https://code.sololearn.com/cAKHg2tm6M14/?ref=app

10th Mar 2023, 12:36 AM
Frankline Njoroge
Frankline Njoroge - avatar
7 Answers
+ 7
Here it is with less code by using string interpolation and a format modifier that specifies a 2-digit field with leading zero and conversion to hexadecimal. rgb1 = int(input()) rgb2 = int(input()) rgb3 = int(input()) print(f"#{rgb1:02x}{rgb2:02x}{rgb3:02x}") 😎 EDIT: It would be better to use variable names r, g, b to clarify which input belongs to which color.
10th Mar 2023, 7:09 AM
Brian
Brian - avatar
+ 10
Frankline Njoroge > some preferred ways to do this task are already shown here. anyway, here 2 other possible solutions: # using a list comprehension rgb = input().split() print('#'+''.join([hex(int(i))[2:].rjust(2, '0') for i in rgb])) # using a modul that serves many conversions from a color representation: https://webcolors.readthedocs.io/en/latest/contents.html# from webcolors import rgb_to_hex # has to be insralled before using it rgb = (map(int, input().split())) print(rgb_to_hex(rgb))
10th Mar 2023, 3:23 PM
Lothar
Lothar - avatar
+ 5
Ion Kare , would you like to share your code with us?
11th Mar 2023, 7:14 AM
Lothar
Lothar - avatar
+ 4
Frankline Njoroge Your code not only was long, it had also flaws. For instance, if there was an input lower than 10, the code crashed because it tried to add a tuple to a string. Also, you don't neccesarily need to use a dictionary. Here is my improvement, hope it will help you! https://code.sololearn.com/cZa7Hjjf0cp5/?ref=app
10th Mar 2023, 7:04 AM
Paleon
Paleon - avatar
+ 4
Brian 's answer is great. f-strings is the way to go. just added some conditionals so that the values never go below 0 or above 255 also, don't use hex as a variable name. It's a reserved keyword and will override the hex() function. It gave me trouble when I was playing with your code and the error message of hex being a list kept popping up...😅. I did not use the hex() function in the end, but still... allrgb = (input(),input(),input()) hexstr = "#" for i in allrgb: i = int(i) v = 0 if i > 0: v = min(i,255) hexstr += f"{v:02x}" print(hexstr)
10th Mar 2023, 2:42 PM
Bob_Li
Bob_Li - avatar
+ 2
Id just write it with an iterator assuming some values could be zeroed out or skipped no point making an rgb converter short lots of invalid input could be made
10th Mar 2023, 10:58 PM
Ion Kare
Ion Kare - avatar
+ 1
rgb1 = int(input()) rgb2 = int(input()) rgb3 = int(input()) allrgb = (rgb1, rgb2, rgb3) hex = [] colors = { 0: "0", 1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8", 9: "9", 10: "a", 11: "b", 12: "c", 13: "d", 14: "e", 15: "f" } for i in allrgb: if i < 10: hex.append(("0", str(i))) elif i < 16: hex.append(("0" + colors[i],)) elif i >= 16: f = colors[i // 16] s = colors[i % 16] hex.append((str(f) + str(s),)) print("#" + hex[0][0] + hex[1][0] + hex[2][0]) ---------------------------------------- i have improved it a bit, hope this help you move forward.
10th Mar 2023, 3:18 AM
Minh Lưu
Minh Lưu - avatar