+ 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.
23 odpowiedzi
+ 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) :)
+ 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)
+ 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")
+ 2
Use print("1")
your string Symbol is wrong
+ 1
use == instead of = and enclose red in double quotes or single quotes
0
the issue is that it says red is not a defined variable when i use “==" @MMatthew tthelénio.RS
0
Dacleary
color = input()
if color == 'red':
print(1)
0
Dacleary right, the red has not been defined.
Enclose red in quotes, or create a variable with the value red
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
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
0
To have an output your input has to be red
0
Erlénio.RS so should i add the other colors as well in the code then test again?
0
Dacleary a variable must never start with a number
0
kk
0
Dacleary it's not necessary, I'm going to do a demo just to see how it works
0
kk
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")
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")
- 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")