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
9/24/2020 11:10:29 AMZone
13 AnswersNew Answer
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)
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".
color = input() if color == "red": print(1) elif color == "green": print(2) ...I'm sure you can fill the rest to account for black.
Abhay Excellent answer.
Rik Wittkopp ty!
Zone oh ok👍
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
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...🤔
color = input() if color == 'red': print('1') elif color == 'green': print('2') elif color == 'black': print('3')
Assigning makes things easy a = input() if a=="red": print(1) elif a=="green": print(2) elif a=="black": print(3)
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!!
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
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 !