Im comfused on why my code aint working. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Im comfused on why my code aint working.

So im busy doing if, else, elif statements and im doing the robot organizing activity where you have to sort the colours into the boxes (red =1, green =2, blue =3) i don’t understand why my code is not working, please explain. my code: color= input() if color =red: print(“1”) thats the part where i get a syntax error.

27th Apr 2021, 8:15 AM
Dacleary
Dacleary - avatar
23 Answers
+ 8
Hello Dacleary, I'm not a python expert but I think you should use in the if statement the '==' to check the equality ('=' is only used in Maths). Try also to convert the input into a string and using the double quotes enclosing the 'red' in the if statement (else your code returns 'red' as a variable, not a string) :)
27th Apr 2021, 8:18 AM
Matthew
Matthew - avatar
+ 6
For comparing two values, use equality (==) operator. color=input() if color=="red": print(1) elif color=="green": print(2) elif color=="blue": print(3)
29th Apr 2021, 7:23 AM
MrMysterious5
+ 2
alright i got it i just messed up the else at the end. heres the working code: color = input() red = "red" green = "green" blue = "blue" black = "black" # code if color == red: print("1") elif color == green: print("2") elif color == blue: print("3") else: print("3")
27th Apr 2021, 8:53 AM
Dacleary
Dacleary - avatar
+ 2
Use print("1") your string Symbol is wrong
29th Apr 2021, 5:18 AM
Himanshu Kumar
Himanshu Kumar - avatar
+ 1
use == instead of = and enclose red in double quotes or single quotes
27th Apr 2021, 8:20 AM
Erlénio.RS
Erlénio.RS - avatar
0
the issue is that it says red is not a defined variable when i use “==" @MMatthew tthelénio.RS
27th Apr 2021, 8:22 AM
Dacleary
Dacleary - avatar
0
Dacleary color = input() if color == 'red': print(1)
27th Apr 2021, 8:22 AM
Erlénio.RS
Erlénio.RS - avatar
0
Dacleary right, the red has not been defined. Enclose red in quotes, or create a variable with the value red
27th Apr 2021, 8:26 AM
Erlénio.RS
Erlénio.RS - avatar
0
color = input() red = "red" green = "green" blue = "blue" # your code goes here if color == red: print("1") it says no output when i test
27th Apr 2021, 8:30 AM
Dacleary
Dacleary - avatar
0
# variables color = input() red = "red" green = "green" blue = "blue" 1 = "1" 2 = "2" 3 = "3" # code if color == red: print(1) when i do it like this it says cannot assign to literal, idk what that error message means
27th Apr 2021, 8:34 AM
Dacleary
Dacleary - avatar
0
To have an output your input has to be red
27th Apr 2021, 8:34 AM
Erlénio.RS
Erlénio.RS - avatar
0
Erlénio.RS so should i add the other colors as well in the code then test again?
27th Apr 2021, 8:36 AM
Dacleary
Dacleary - avatar
0
Dacleary a variable must never start with a number
27th Apr 2021, 8:36 AM
Erlénio.RS
Erlénio.RS - avatar
0
kk
27th Apr 2021, 8:38 AM
Dacleary
Dacleary - avatar
0
Dacleary it's not necessary, I'm going to do a demo just to see how it works
27th Apr 2021, 8:38 AM
Erlénio.RS
Erlénio.RS - avatar
0
kk
27th Apr 2021, 8:41 AM
Dacleary
Dacleary - avatar
0
but now it says if i get black as an input i must also output 3 but my code now has an invalid syntax again, sorry for being a noob. # variables color = input() red = "red" green = "green" blue = "blue" black = "black" # code if color == red: print("1") elif color == green: print("2") elif color == blue: print("3") else: print("3")
27th Apr 2021, 8:43 AM
Dacleary
Dacleary - avatar
0
color = input("Enter the color to organize:") if color == "red": print("1") elif color == "green": print("2") elif color == "blue": print("3") else: print("not organized")
27th Apr 2023, 9:58 AM
Jonas Drik
Jonas Drik - avatar
- 1
Dacleary Hii. Unless untill it is asked to do so, assiging the red again to a variable red and so on would only increase computation(though negligible in this case) and when number of colors increase you would have to increase number of variables. #code color = input() if color == "red": print("1") elif color == "green": print("2") elif color in ["blue", "black"]: """"checks whether color is blue or black simultaneously.""" print("3") else: print(color, "is not a valid color")
28th Apr 2021, 3:43 AM
Samuel Luke
Samuel Luke - avatar