0
Red and green don't work but black does can anyone help me fix please?
If you enter red or green it shows the output box number correct but also shows unknown. Don't know why and all other colors work correctly as showing just unknown. Please assist if you can. https://code.sololearn.com/c48uGY6bZ22x/?ref=app
4 Antworten
+ 8
Corrected code:
color = input()
if color=="red":
print("box #1")
elif color=="green":
print("box #2")
elif color=="black":
print("box #3")
else:
print("unknown")
- no need for parenteses around input
- if you want only a single condition to evaluate, use "elif" in the middle
- use consistent indentation (4 characters everywhere is usual)
+ 3
Hello tibor Santa thank you very much for the help. And you mean four spaces for indentation correct?
+ 2
Yes, 4 spaces.
+ 1
Tibor Santa is right!
But you can use dictionary here instead using too many if-else condition.
color = input()
box = {
"red":"box #1",
"green":"box #2",
"black":"box #3"
}
print(box.get(color, "unknown"))