+ 1
Elif Statements / Python
https://www.sololearn.com/coach/126?ref=app Write a program that will be used in a robot that categorizes items by their color. Each color corresponds to a box with a specific number. For simplicity, our program will handle 3 colors: red goes to box #1 green goes to box #2 black goes to box #3 Your program needs to take a color as input and output the corresponding box number. Sample Input green Sample Output 2
17 Answers
+ 5
Because you are doing it wrong ,why are you taking color as input when you are not going to use it anywhere?
and
(if red
elif green
elif black),
all results in true ,only the first condition will be checked resulting in output as always 1 according to your solution
Program is asking to check if color is green than output 2 ,if red output 1 and if black output 3
if color=="red":
print(1)
elif color=="green":
print(2)
elif color=="black":
print(3)
+ 7
Try writing:
if color=="Red":
print(1)
elif color=="Green" :
print(2)
And the same for black.
As the variable color is the input and it should be Red/Green/Black, you have to write: if color=="Red/Green/Black".
+ 5
color = input()
if color == "red":
print(1)
elif color == "green":
print(2)
...I'm sure you can fill the rest to account for black.
+ 3
Abhay
Excellent answer.
+ 2
Rik Wittkopp ty!
+ 1
Zone oh ok👍
+ 1
a = 'red'
b = 'green'
c = 'black'
color = input()
if color == a:
print('1')
elif color == b:
print('2')
elif color == c:
print('3')
try this, it is working
+ 1
color = input()
if color=="red":
print ("1")
elif color=="green":
print ("2")
else:
print("3")
+ 1
color=input('Enter the color: ')
color1='red'
color2='green'
color3='black'
if(color==color1):
print('Box 1')
elif(color==color2):
print('Box 2')
elif(color==color3):
print('Box 3')
else:
print('Theres no such box corresponding to given color')
0
I tried
color = input()
# your code goes here
red = 1
green = 2
black = 3
if red:
print(1)
elif green:
print(2)
elif black:
print(3)
And it comes out wrong...🤔
0
Abhay i was taking color as input because that's what the challenge was telling me to do. Your answer works. I had tried yours before but didn't put the colors in string quotes. Thanks !
0
Assigning makes things easy
a = input()
if a=="red":
print(1)
elif a=="green":
print(2)
elif a=="black":
print(3)
0
I tried the below and was incorrect..
if color == red:
print("1")
elif color == green:
print("2")
elif color == black:
print("3")
I can see the difference here...
if color=="red":
print(1)
elif color=="green":
print(2)
elif color=="black":
print(3)
But I can't understand the difference haha.. any tips will be appreciated!!
0
Riley
Your first code outputs type string.
Your 2nd code outputs type integer.
A string character is similar to text, letters that form a message.
An integer is a number which may be used to resolve math problems.
Strange as it may seem, think of "2" as the letter 2
Think of 2 as the number 2
PS: This query should have been in a separate post
0
//your code goes here
Scanner sc = new Scanner(System.in);
String color = sc.nextLine();
switch(color){
case "red":
System.out.println(1);
break;
case "green":
System.out.println(2);
break;
case "black":
System.out.println(3);
}
0
Organized Robot🤖
You are developing a program that will be used in a robot that categorizes items by their color.
Each color corresponds to a box with a specific number.
For simplicity, our program will handle 3 colors:
🔴 red goes to box #1
🟢 green goes to box #2
⚫ black goes to box #3
Your program needs to take color as input and output the corresponding box in the given format.
In the case of an unsupported colors, the program should output unknown.
Input Example⬅️: green
Output Example➡️: box #2