+ 1
Tuple related (python)
def cmyk_to_rgb (): x = [] for i in range(4): x.append(float(input())) c,m,y,k= x r = round(255 * (1 - c) * (1 - k)) g = round(255 * (1 - m) * (1 - k)) b = round(255 * (1 - y ) * (1 - k)) ''' result = r,g,b return result #the above lines returns a tuple. #example outputs (1,2,3) instead of 1,2,3''' return str(r)+","+str(g)+","+ str(b) print (cmyk_to_rgb()) Refer to the comment part. Since, it was returning a tuple with parentheses, I had to convert all manually to string and then add two commas in between. Is there a way I could have returned the result in the desired format i.e. 1,2,3 without doing this ??
3 Respuestas
+ 4
CHANDAN ROY
That print I did is just another method or alternative, it has no connection with your function.
And with your function, you can use f-strings or concatenation using + (like what you did) instead of comma because comma will generate a tuple.
(1,2,3) ---> tuple
1,2,3 ---> tuple
https://code.sololearn.com/c5aPiYyvv9To/?ref=app
+ 4
",".join([str(x) for x in input.split()])
>> 1,2,3,4
#.join method convert lists into string with "," between elements, you can join characters whatever you want.
- - - - - - - - - - - -
OR
print(r, g, b, sep=",")
>> 1,2,3
# sep parameter in print, represents the comma that separates each variables. By dafult sep is whitespace but you can costumize or use whatever character you want.
- - - - - - - - - - - -
And the reason why it outputs tuple is because when Unpacking values and variables, the unpacked or zipped value will return Tuple.
+ 1
《 Nicko12 》 print statement also generates None as function doesn't return anything