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

时间:2014-04-10 13:52:53

标签: python text input lowercase

((回应上述编辑,上述链接没有回答。上述问题与我的预期用途无关。))

我已经阅读了一个关于将字符串转换为小写字母的类似问题;

How to convert string to lowercase in Python

我理解这是如何完美的,但是我自己的尝试失败了。

这是我当前调试块的设置示例;

#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

但是使用这个块只需要我已经的代码行,并将其移动到其他地方。我知道我可以设置它,如果它不是'是',那么else可以自动设置devstate为0,但我更喜欢有一个更受控制的环境。我不想意外地用空格键入“是”并且已经关闭了。

回到这个问题;

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

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论坛的原因是因为我更喜欢以自己的方式提出我的问题,而不是试图将一个问题的答案拼凑起来,而这个问题对于其他人来说是不同的。))

3 个答案:

答案 0 :(得分:6)

使用.lower()是您的最佳选择

choice = input()
choice = choice.lower()
if choice == 'yes':
    dev_debug = 1
    break

或者在'

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

答案 1 :(得分:2)

您可以在某个变量中添加一个小写值,并使用该值代替原始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?")

答案 2 :(得分:0)

单词回答: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']