【问题标题】:Text Game - Convert input text to lowercase - Python 3.0 [duplicate]文本游戏 - 将输入文本转换为小写 - Python 3.0 [重复]
【发布时间】:2014-04-10 13:52:52
【问题描述】:

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

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

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 语句,而不是使用两个大写。我确实有一个想法,使用另一个块来确定 True of False 状态,看起来像这样;

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,但我更喜欢有一个更可控的环境。我不想不小心输入带有空格的“yes”并关闭开发模式。

回到问题上来;

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

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 的代码。
  • 只使用 ifchoice.lower() == "yes" 就完成了
  • @Kevin .lower 的位置让我感到困惑。 “如何在 python 中将字符串转换为小写”对我来说,并没有向我大喊我应该把 .lower 放在哪里
  • @Wooble 我看了那个问题,但它对我没有任何帮助。
  • @GameWylder 您看不到您的choice == "yes" or "Yes" 条件实际上是(choice == "yes") or ("Yes") - 其中"Yes" 部分将始终 评估为True - 阅读后一个带有解决方案的问题,确切地解释了为什么这不是写你想要的东西的方式?

标签: python text input lowercase


【解决方案1】:

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

【讨论】:

  • 我试了第一个例子,效果很好!感谢第二个示例,这绝对是我将来会使用的东西。再次感谢您或显示正确的设置。
  • @GameWylder 第二个例子与 Wooble 链接到的问题的公认解决方案完全相同相同。
【解决方案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?")

【讨论】:

    【解决方案3】:

    一句话回答: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']
    

    【讨论】:

      猜你喜欢
      • 2010-12-16
      • 1970-01-01
      • 2018-12-17
      • 2020-04-07
      • 2011-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-02
      相关资源
      最近更新 更多