[Tutor] Function - Python

Mats Wichmann mats at wichmann.us
Fri Apr 10 07:33:20 EDT 2020


On April 10, 2020 3:10:34 AM MDT, Zulfadli Azhar <zulfadli.azh at gmail.com> wrote:
>Hi Alan,
>
>I just started to learn Python this week. I have some confusion here.
>Please see below the code that I created. I supposed to come out with a
>Return #0000ff but it's not.
>
>def color_translator(color):
>if color == "red":
>hex_color = "#ff0000"
>elif color == "green":
>hex_color = "#00ff00"
>elif color == "blue":
>hex_color = "#0000ff"
>else:
>hex_color = "unknown"
>return "blue"
>
>print(color_translator("blue")) # Should be #0000ff
>print(color_translator("yellow")) # Should be unknown
>print(color_translator("red")) # Should be #ff0000
>print(color_translator("black")) # Should be unknown
>print(color_translator("green")) # Should be #00ff00
>print(color_translator("")) # Should be unknown
>
>Please do not tell me a direct answer but I need an explanation on how
>to
>come out with a Return #0000ff.
>
>I have 3 more questions to ask as I have spent my whole day today to
>figure
>get the correct code by referring from other sources.
>
>Thank you
>ZUL
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>To unsubscribe or change subscription options:
>https://mail.python.org/mailman/listinfo/tutor

when you call a function, it returns a value. the function can set that with the return statement, or if it doesn't have one, the special value None is returned. look at what you are returning... you take the trouble to set hex_value, but that isn't what you return.
-- 
Sent from a mobile device with K-9 Mail. Please excuse my brevity.


More information about the Tutor mailing list