+ 2
Unexpected output
Help me, please, python throws a output but it's a mistake https://code.sololearn.com/cuya81eP9q4J/?ref=app
3 odpowiedzi
+ 1
this program will give your suitable output
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 hex_color
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
+ 6
You can use a dictionary like a switch;
def color_translator(color):
return {"red": "#ff0000",
"green": "#00ff00",
"blue": "#0000ff"}.get(color, "unknown")
+ 1
Change line 8 to
" else:"
to have a default case