文本游戏 - 将输入文本转换为小写 - Python 3.0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22990069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Text Game - Convert input text to lowercase - Python 3.0
提问by Anthony Dragone
((In response to the above edit, this was not answered in the above link. The above question is irrelevant to my intended use.))
((针对上面的编辑,上面的链接没有回答这个问题。上面的问题与我的预期用途无关。))
I have read a similar question about turning a string into lowercase;
我读过一个关于将字符串转换为小写的类似问题;
How to convert string to lowercase in Python
I understand how this works perfectly, however my attempts at this myself have failed.
我明白这是如何完美运作的,但是我自己在这方面的尝试失败了。
Here's my current setup example for a debug block;
这是我当前的调试块设置示例;
#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?")
#
So, I have it prewritten to work with a different capitalization. However, I'd like to use only one if
statement per word rather than using two for the capitalization. I do have an idea, that uses another block to determine a True of False state, which would look like this;
所以,我预先编写了它以使用不同的大写字母。但是,我想if
每个单词只使用一个语句,而不是使用两个大小写。我确实有一个想法,它使用另一个块来确定 True 或 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
But using this block would just take the lines of code I alreadyhave, and move it somewhere else. I know I could set it up that if it isn't 'Yes' then the else can automatically set devstate to 0, but I prefer to have a more controlled environment. I don't want to accidentally type 'yes ' with a space and have devmode off.
但是使用这个块只会占用我已经拥有的代码行,并将它移到其他地方。我知道我可以设置它,如果它不是“是”,那么 else 可以自动将 devstate 设置为 0,但我更喜欢有一个更可控的环境。我不想不小心在空格中输入“是”并关闭 devmode。
So back to the question;
所以回到问题;
How would I make it so I can just do the following?
我将如何做到这一点,以便我可以执行以下操作?
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?")
#
The above code probably isn't the best example, but it at least helps me get my point across when I say I only want one if
statement per word. (Also, I hope I didn't just solve my own problem here xD.)
上面的代码可能不是最好的例子,但是当我说if
每个单词只需要一个语句时,它至少可以帮助我理解我的观点。(另外,我希望我不只是在这里解决了我自己的问题 xD。)
So, how would I do this?
那么,我该怎么做呢?
((Also, the reason I go here rather than the Python forums is because I prefer to ask my question in my own way rather than trying to piece together an answer from a question that was worded differently for someone else.))
((另外,我去这里而不是 Python 论坛的原因是因为我更喜欢以我自己的方式提出我的问题,而不是试图从其他人的措辞不同的问题中拼凑出一个答案。))
采纳答案by Rod Hyde
Using .lower() is your best option
使用 .lower() 是你最好的选择
choice = input()
choice = choice.lower()
if choice == 'yes':
dev_debug = 1
break
Or use 'in'
或使用“在”
choice = input()
if choice in ('yes', 'Yes'):
dev_debug = 1
break
回答by Jan Spurny
You may put a lowercased value in some variable and use that instead of original choice
where you don't care about case and you can still use choice
where the case is significant:
您可以在某个变量中放置一个小写的值,并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?")
回答by Neerav
One word answer: string.lower()
一字答案:string.lower()
To find out what variables/methods are available for a class/object, just use dir(object/class/method())
要找出类/对象可用的变量/方法,只需使用 dir(object/class/method())
For e.g.
例如
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']