1

I have an RGB LED connected to a Raspberry Pi 3 with the below code. What I want to do is present the user with a question to choose Red, Green or Blue, corresponding to a variable linked to specific GPIO pins.

When user enters, Red, the LED will turn red. When they enter Blue, the LED will turn blue.

Currently if I enter red, the code will print '20' (integer), which corresponds to the BCM pin 20. This is good, but my problem is that I'm having trouble converting the user's string response to lowercase first. (i.e., convert RED to red).

I am getting an error:

request =  input("Choose a color. Red/Green/Blue".lower())
File "<string>", line 1, in <module>
NameError: name 'Red' is not defined

The code below is at its simplest form to first test that I can get the lowercase input from the user.

red = 20
green = 16
blue = 21

try:
    while True:
        # I would like to convert user's answer (Red, Green,Blue) to a lowercase answer (ie. red, green blue)
        request =  input("Choose a color. Red/Green/Blue").lower()

        print(type(request))
        print(request)

except KeyboardInterrupt:

Any help would be much appreciated.

theAussieGuy
  • 157
  • 1
  • 2
  • 11
  • 2
    Are you sure you're using Python 3? – melpomene Jul 17 '16 at 22:40
  • 1
    Possible duplicate of [Python input() error - NameError: name '...' is not defined](http://stackoverflow.com/questions/21122540/python-input-error-nameerror-name-is-not-defined) – zondo Jul 17 '16 at 22:41
  • Thank you...As far as I can tell it's python 3. I've read the article: [Python input() error - NameError: name '…' is not defined](http://stackoverflow.com/questions/21122540/python-input-error-nameerror-name-is-not-defined), but it doesn't really answer my question. I can get a similar result if I change to raw_input("Question here"), but then I have to write extra code. I was hoping to automatically change the user's input to lowercase which is then translated to the variable's value of either 16, 20, 21. I hope that makes sense. – theAussieGuy Jul 17 '16 at 23:00
  • Are you sure you want input( .lower()) not input().lower() ? – UpmostScarab Jul 17 '16 at 23:15
  • 1
    A better way is to use a dictionary. Something like `colors = {'red': 20, 'green': 16, 'blue': 21}`. Then, use `colors[raw_input(...).lower()]` – zondo Jul 17 '16 at 23:24
  • Thanks Zondo. That worked a treat. Exactly what I was after. – theAussieGuy Jul 18 '16 at 01:32
  • 1
    It's a pleasure; I'm glad I could help. Note that you will get errors if the user types something wrong. Of course, the correct way to deal with it is probably to use `try`-`except` blocks, but you could also use `colors.get(raw_input(...).lower())` to have None as a default, or provide a second argument to `.get()` to use that as the default instead. – zondo Jul 18 '16 at 01:33
  • Ok. excellent. Thanks – theAussieGuy Jul 18 '16 at 01:38

1 Answers1

2

This is not Python3. Python's 3 "input" would return you a string, which you could then convert to lowercase - but your code does not have anything in it for, given a string with the color name, retrieve the cotnents associated with the variable with that same name.

Python 2's input, on the other hand, performs an eval, running whatever the user types in as a Python expression, before returning the result. Therefore, when the user types in red it will give you theassociated value 20. (And a lower call on this value would fail).

What you have to do there is: Write code that will work in Python2 or Python3, and second, make a consistent mechanism to retrieve your color given a user-typed string. For this last part, the recomended way is to associate your color names and values using a dictionary mapping, rather than outright as variables.

So:

try:
   input = raw_input 
except NameError: 
   pass

colors = dict(
    red = 20,
    green = 16,
    blue = 21,
)

try:
    while True:
        request =  input("Choose a color. Red/Green/Blue")
        color = colors[request.lower()]
        ...
    except NameError:
        print("Invalid color name selected")
    except KeyboardInterrupt:
        ...
jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • Thank you very much. That's very easy to understand. In the 'while True;' section of code, you wrote 'color = color[request.lower()] should that be color = colors[request.lower()] ? (with an 'S') – theAussieGuy Jul 18 '16 at 20:11