1

I'm trying to write a program that will ask the user to input a color and depending on the color they pick, python converts it to a number and prints that number.

Here's my code:

def main():
brown1 = 5
red1 = 6
orange1 = 3 
color1 = input("Color on resistor? (separated by comma) : ")
if color1 == "Brown" or "brown":
    color1 = brown1
    if color1 == "Red" or "red":
        color1 = red1   
        if color1 == "Orange" or "orange":
            color1 = orange1
print(color1)
main()

The problem is that Python only returns the last color, that is, 3. I'm just starting to program and I don't really know what else to do to this code for it to work. Any suggestions? Thanks.

user2774311
  • 11
  • 1
  • 2
  • Instead of using `if` everytime, use `elif`, that way only one of your assignement statements will be triggered. Also, the indentation of those if/elif statements should be equal – Tyler Sep 12 '13 at 21:24

4 Answers4

4

You problem is that the if statements are nested, when they should not. An inner if is only reached when the outer condition is true

You can also use a dict:

colors = {'brown': brown1, 'red': red1, 'orange': orange1}
color1 = colors[color1.lower()]
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
3

The problem with your code is that you have put if inside other if. So if the first condition satisfies then only the second condition is checked and so on. And also you have used color1 == "Brown" or "brown", this will always return True because boolean of a non-empty string is True. You should do something like this:

def main():
    brown1 = 5
    red1 = 6
    orange1 = 3 
    color1 = input("Color on resistor? (separated by comma) : ")
    if color1 == "Brown" or color1 == "brown":
        color1 = brown1
    elif color1 == "Red" or color1 == "red":
        color1 = red1   
    elif color1 == "Orange" or color1 == "orange":
        color1 = orange1
    print(color1)
main()

A better solution would be to use dictionary.

color_dict = {'brown':5, 'red':6 ,'orange':3}
color1 = input("Color on resistor? (separated by comma) : ")
print color_dict[color1.lower()]
Ankur Ankan
  • 2,953
  • 2
  • 23
  • 38
0

The problem with your code is that the if statements should be like this:

if color1 == "Brown" or color1 == "brown":
    ...

This is because "brown" is a non-empty string, so it returns True, which means your if condition will always be true. So, it keeps entering the if statements till it reaches the last one.

Here's how we'd correct this particular mistake:

1. brown1 = 5
2. red1 = 6
3. orange1 = 3 
4. color1 = input("Color on resistor? (separated by comma) : ")
5. if color1 == "Brown" or color1 == "brown":
6.     color1 = brown1
7.     if color1 == "Red" or color1 == "red":
8.         color1 = red1   
9.         if color1 == "Orange" or color1 == "orange":
10.             color1 = orange1
11. print(color1)

Let's try to dry-run this program (manually go line-by-line to see what's happening). if color1 = "brown" then we'll enter the if statement at line 5. Then we'll go to line 6. This means color1 is defined to be 5 now. Thus, color1 can never be "Red" or "red". Additionally, if color1 is not "brown" or "Brown", then it'll go to the next line with the same indentation, or 11, which means it'll never check for red and orange.

What you want to accomplish can be done like this:

if color1 == "brown" or color1 == "Brown":
    color = 5
if color1 == "brown" or color1 == "Brown":
    color1 = 6
if color1 == "brown" or color1 == "Brown":
    color1 = 3

Now, a problem with this is that if we change the value of color1 in the lines 2 or 4, the remaining conditions may be true even though you don't want to check them again (For example: if color==1: color=2; if color==2: color=1 resets color to 1 because the second condition is true once more). This is why elif or else if is recommended, making our new program:

if color1 == "brown" or color1 == "Brown":
    color = 5
elif color1 == "brown" or color1 == "Brown":
    color1 = 6
elif color1 == "brown" or color1 == "Brown":
    color1 = 3
shashwat
  • 992
  • 1
  • 13
  • 27
  • 2
    The indented `if` statements are still a problem, if you input 'orange' you will never even make it to the assignment statement on line **10**, because you would've had to pass the checks on lines **5** and **7**, meaning that the color would have to be brown, red, and orange. – Tyler Sep 12 '13 at 21:31
  • I like the idea of dry-running the program. I hadn't thought of doing that. – user2774311 Sep 14 '13 at 01:57
0

I'd add a check that color1 belongs to colors to Lev Levitsky's solution. To avoid ugly KeyError exceptions.

def main():
  color = {"brown": 5, "red": 6, "orange": 3}
  c = input("Color on resistor? (separated by comma) : ")
  c = c.lower()
  if c in color:
    print(color[c])
  else:
    print("Color not found")

Optionally the same can be written in a pythonic way

def main():
  color = {"brown": 5, "red": 6, "orange": 3}
  c = input("Color on resistor? (separated by comma) : ")
  c = c.lower()
  try:
    print(color[c])
  except KeyError:
    print("Color not found")
Dmitriy
  • 340
  • 2
  • 9
  • you can also use `color.get(c, None)` or some other sensible default value. – tacaswell Sep 13 '13 at 04:36
  • This made it work! Two questions: what difference is there between braces and parentheses? and what does the .lower() do? – user2774311 Sep 14 '13 at 01:56
  • @user2774311 `string.lower(s)` Return a copy of s, but with upper case letters converted to lower case. a trick to handle 'Brown', 'brown', 'BrOwN' etc. with one check – Dmitriy Sep 15 '13 at 11:27