Elif Statements / Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

24th Sep 2020, 11:10 AM
Zone
Zone - avatar
16 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)
24th Sep 2020, 11:35 AM
Abhay
Abhay - avatar
+ 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".
24th Sep 2020, 11:36 AM
Pranav Kalro
Pranav Kalro - avatar
+ 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.
24th Sep 2020, 11:37 AM
Russ
Russ - avatar
+ 3
Abhay Excellent answer.
24th Sep 2020, 11:46 AM
Rik Wittkopp
Rik Wittkopp - avatar
24th Sep 2020, 12:34 PM
Abhay
Abhay - avatar
+ 1
Zone oh ok👍
24th Sep 2020, 12:34 PM
Abhay
Abhay - avatar
+ 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
27th Jun 2021, 11:01 PM
Hugues Rodrigue Tha Andela
Hugues Rodrigue Tha Andela - avatar
+ 1
color = input() if color=="red": print ("1") elif color=="green": print ("2") else: print("3")
16th Oct 2021, 6:01 AM
Srikanta Rawlo
Srikanta Rawlo - avatar
+ 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')
21st Dec 2021, 3:35 PM
SANVIKA
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...🤔
24th Sep 2020, 11:11 AM
Zone
Zone - avatar
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 !
24th Sep 2020, 12:08 PM
Zone
Zone - avatar
0
Assigning makes things easy a = input() if a=="red": print(1) elif a=="green": print(2) elif a=="black": print(3)
9th Apr 2021, 7:11 AM
Veena Tirmal
Veena Tirmal - avatar
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!!
6th Aug 2021, 11:48 PM
Riley
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
7th Aug 2021, 12:00 AM
Rik Wittkopp
Rik Wittkopp - avatar
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); }
11th Aug 2023, 6:28 AM
Ghulam Qambar Dobal
Ghulam Qambar Dobal - avatar
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
26th Aug 2023, 4:53 AM
NARESH M
NARESH M - avatar