elif Statements help needed please | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

elif Statements help needed please

I am doing the pro questions for elif statements. The question is; 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 The code I wrote was: color = input() red = 1 green = 2 black = 3 if color == red: print("1") elif color == green: print("2") elif color == black: print("3") However i get "No Output" Any help would be appreciated.

18th Jan 2021, 1:55 AM
jk
jk - avatar
6 Answers
+ 2
ah wait until you get to the elif V, i am stuck on that one! its tough.
19th Jan 2021, 2:53 AM
Steve Barone
Steve Barone - avatar
+ 1
input() returns an object of type string. All of your comparisons will be False since you are asking if: "green" == 1 "green" == 2 "green" == 3 instead remove the red, green, black variables as they aren't really needed. Then just compare the color variable to the corresponding strings and leave your print statements the same. if color == "red": print(1) ... ... ... ... Etc
18th Jan 2021, 2:32 AM
ChaoticDawg
ChaoticDawg - avatar
0
Thank you so much! I tried that the first time but missed out the quotation marks. Greatly appreciated
18th Jan 2021, 3:06 AM
jk
jk - avatar
0
The question describes a scenario where you are building a robot that categorizes items by their color. Each color corresponds to a specific box number, and you are tasked with creating a program to determine the box number based on the color input. The problem specifies that there are three colors: red, green, and black. The program should take a color as input, and then output the corresponding box number according to the following mapping: red goes to box #1 green goes to box #2 black goes to box #3 The user will provide the color as input, and the program should process this input to find the appropriate box number. For example, if the user enters "green" as the color, the program will recognize that "green" corresponds to box number 2 and should output: 2 This means that the item with the color "green" should be placed in box number 2. import java.util.Scanner; public class Program { public static void main(String[] args) { // Your code goes here Scanner scanner = new Scanner(System.in); String color = scanner.nextLine(); int boxNumber; // Use switch case to determine the box number based on the color switch (color) { case "red": boxNumber = 1; break; case "green": boxNumber = 2; break; case "black": boxNumber = 3; break; default: boxNumber = -1; // If an invalid color is entered break; } // Output the corresponding box number if (boxNumber != -1) { System.out.println(boxNumber); } else { System.out.println("Invalid color entered!"); } } } Credit: www.lawtantra.org
2nd Aug 2023, 7:29 AM
Novi
Novi - avatar
0
def main(): color = input("Enter the color: ") boxNumber = -1 # Use if-elif-else statements to determine the box number based on the color if color == "red": boxNumber = 1 elif color == "green": boxNumber = 2 elif color == "black": boxNumber = 3 # Output the corresponding box number if boxNumber != -1: print(boxNumber) else: print("Invalid color entered!") if __name__ == "__main__": main() In this Python program, we use input() to read the user's input for the color. The program then uses if-elif-else statements to determine the box number based on the color entered by the user. If the input color matches one of the specified cases, it will set the corresponding boxNumber. If the color is not recognized, it will remain as -1. The program will output the corresponding box number if a valid color is entered. Otherwise, if an invalid color is entered, it will print "Invalid color entered!". Credit: www.lawtantra.org
2nd Aug 2023, 7:33 AM
Novi
Novi - avatar
0
let color = readLine(); /* red => 1 green => 2 black => 3 other cases => unknown */ //your code goes here if (color === 'red') { console.log(1); } else if (color === 'green') { console.log(2); } else if (color === 'black') { console.log(3); } else { console.log('unknown'); } this is javascript version of it
5th Jan 2024, 2:52 AM
Sarkodie Andrews
Sarkodie Andrews - avatar