Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
文本游戏-将输入文本转换为小写-Python 3.0_Python_Text_Input_Lowercase - Fatal编程技术网

文本游戏-将输入文本转换为小写-Python 3.0

文本游戏-将输入文本转换为小写-Python 3.0,python,text,input,lowercase,Python,Text,Input,Lowercase,((作为对上述编辑的回应,上述链接中未回答此问题。上述问题与我的预期用途无关。) 我读过一个关于将字符串转换成小写的类似问题 我完全理解这是如何工作的,但我自己的尝试失败了 下面是我当前调试块的设置示例 #Debug block - Used to toggle the display of variable data throughout the game for debug purposes. def debug(): print("Would you like to play i

((作为对上述编辑的回应,上述链接中未回答此问题。上述问题与我的预期用途无关。)

我读过一个关于将字符串转换成小写的类似问题

我完全理解这是如何工作的,但我自己的尝试失败了

下面是我当前调试块的设置示例

#Debug block - Used to toggle the display of variable data throughout the game for debug purposes.
def debug():
    print("Would you like to play in Debug/Developer Mode?")
    while True:
        global egg
        choice = input()
        if choice == "yes":
            devdebug = 1
            break
        elif choice == "Yes":
            devdebug = 1
            break
        elif choice == "no":
            devdebug = 0
            break
        elif choice == "No":
            devdebug = 0
            break
        elif choice == "bunny":
            print("Easter is Here!")
            egg = 1
            break
        else:
            print("Yes or No?")
 #
因此,我预先编写了它,以便使用不同的大写字母。但是,我希望每个单词只使用一个
if
语句,而不是使用两个来表示大小写。我确实有一个想法,使用另一个块来确定真或假状态,看起来是这样的

def debugstate():
    while True:
        global devstate
        choice = input()
        if choice == "Yes":
            devstate = True
            break
        elif choice == "yes":
            devstate = True
            break
        elif choice == "No":
            devstate = False
            break
#Etc Etc Etc
但是使用这个代码块只需要将我已有的代码行移到其他地方。我知道我可以设置它,如果它不是“Yes”,那么else可以自动将devstate设置为0,但我更喜欢一个更受控制的环境。我不想意外地键入带有空格的“yes”并关闭devmode

回到问题上来

我该怎么做才能做到以下几点

def debug():
    print("Debug Mode?")
    while True:
        global egg
        choice = input()
        if choice == "yes" or "Yes":
            devdebug = 1
            break
        elif choice == "no" or "No":
            devdebug = 0
            break
        elif choice == "egg":
            devdebug = 0
            egg = 1
            print("Easter is Here")
            break
        else:
            print("Yes or No?")
#
上面的代码可能不是最好的例子,但当我说每个单词只需要一个
if
语句时,它至少帮助我理解了我的观点。(另外,我希望我没有在这里解决我自己的问题xD。)

那么,我该怎么做呢

(另外,我之所以选择这里而不是Python论坛,是因为我更喜欢以自己的方式提问,而不是试图从一个对其他人来说措辞不同的问题中拼凑出答案。)

使用.lower()是最好的选择

choice = input()
choice = choice.lower()
if choice == 'yes':
    dev_debug = 1
    break
或者使用“in”

choice = input()
if choice in ('yes', 'Yes'):
    dev_debug = 1
    break

您可以在某个变量中输入小写值,并在不关心大小写的情况下使用该值而不是原始的
choice
,在大小写重要的情况下,您仍然可以使用
choice

def debug():
    print("Debug Mode?")
    while True:
        global egg
        choice = input()
        lowchoice = choice.lower()
        if lowchoice == "yes":
            devdebug = 1
            break
        elif lowchoice == "no":
            devdebug = 0
            break
        elif choice == "egg":
            devdebug = 0
            egg = 1
            print("Easter is Here")
            break
        else:
            print("Yes or No?")

一个词的答案:string.lower()

要找出类/对象可用的变量/方法,只需使用dir(object/class/method())

例如

a="foo"
type(a)
<type 'str'>
dir(a)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
a=“foo”
类型(a)
处长(a)
“UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU"乐","乐","乐","乐","乐","乐","乐","乐","乐","乐,"乐","乐,"乐,"乐,","乐,","乐,"乐,,"乐,,"乐,34etattr_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,'rfind','rindex','rjust','rpartition','rsplit','rstrip','splitlines','startswith','strip','swapcase','title','translate','upper','zfill']

“我理解这是如何完美工作的,但我自己的尝试失败了”。我很想看看您试图使用
.lower
的代码。只要使用if choice.lower()=“yes”就可以了done@Kevin让我困惑的是,放低了。“如何在python中将字符串转换为小写”对我来说,并没有向我大声说出我将把它放在哪里。lower@Wooble我看了那个问题,它对我没有任何帮助。@GameWylder你看不到你的
选择==“yes”或“yes”
条件实际上是
(选择==“yes”)或(“yes”)
-其中
part的评估结果总是
True
——在阅读了一个问题并给出了解决方案,解释了为什么这不是编写所需内容的方法之后?我尝试了第一个示例,它非常有效!感谢第二个例子,这肯定是我将来会用到的。再次感谢您或显示正确的设置。@GameWylder第二个示例与Wooble链接到您的问题的公认解决方案完全相同。