Elif Statements / Python | Sololearn: Learn to code for FREE!

0

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 AM

Zone

13 Answers

New Answer

+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.

+2

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

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

color = input() if color == 'red': print('1') elif color == 'green': print('2') elif color == 'black': print('3')

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

-1

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 !