【发布时间】: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