简体   繁体   English

Raspberry Pi在Python 3脚本中的lower()函数未将输入转换为小写

[英]lower() function not converting input to lowercase in python 3 script for Raspberry Pi

I have an RGB LED connected to a Raspberry Pi 3 with the below code. 我使用以下代码将RGB LED连接到Raspberry Pi 3。 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. 我想做的是向用户提出一个问题,以选择红色,绿色或蓝色,对应于链接到特定GPIO引脚的变量。

When user enters, Red, the LED will turn red. 当用户输入红色时,LED将变为红色。 When they enter Blue, the LED will turn blue. 当他们输入蓝色时,LED将变为蓝色。

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. 当前,如果我输入红色,代码将打印'20'(整数),它对应于BCM引脚20。这很好,但是我的问题是我无法先将用户的字符串响应转换为小写。 (ie, convert RED to red) . (即,将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. 任何帮助将非常感激。

This is not Python3. 这不是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的3个“输入”将返回一个字符串,然后可以将其转换为小写字母-但是在给定颜色名称的string ,代码中没有任何内容,因此可以检索与具有相同名称的变量相关联的cotnent。

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. 另一方面,Python 2的输入执行eval ,然后在返回结果之前运行用户以Python表达式键入的内容。 Therefore, when the user types in red it will give you theassociated value 20 . 因此,当用户键入red ,它将为您提供关联值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. 您所要做的是:编写可在Python2或Python3中工作的代码,其次,在给定用户键入的字符串的情况下,建立一种一致的机制来检索颜色。 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:
        ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM