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

0 投票
3 回答
53442 浏览
提问于 2025-04-18 02:12

(针对上面的编辑,这个问题在上面的链接中没有得到回答。上面的问题与我想要的用途无关。)

我看过一个类似的问题,关于如何把字符串变成小写;

如何在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

但是使用这个代码块只是把我已经写好的代码移动到别的地方。我知道我可以设置,如果不是'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论坛,是因为我更喜欢用自己的方式提问,而不是试图从别人用不同措辞提出的问题中拼凑出答案。)

3 个回答

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']
2

你可以把一个小写的值放在某个变量里,然后在不在乎大小写的地方用这个变量,至于原来的 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?")
8

使用 .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

撰写回答