问题描述
嗯,我的while循环用或似乎没有工作,因为我想要...
如何告诉while循环只接受Y或y或y。或N或N。从str(raw_input)输入或n
输入?
提前感谢'
代码片段:
导入os
def buildfinder():
os.system(" ; CLS")
GameRoot = os.getenv(" GAME_ROOT")+" \\"
打印你想要吗?使用 + GameRoot +"作为你的来源
目录?"
usr = str(raw_input(''Y / N:''))
返回usr
#Runs构建器功能
usrinp = buildfinder()
def buildwhiler():
而usrinp!=" y"或Y或Y。或N或N。或n:<<<< ----问题
print"输入Y或N!"
usr = str(raw_input (''Y / N:''))
否则:
代码继续
Hmm, my while loop with "or" doesn''t seem to work as I want it to...
How do I tell the while loop to only accept "Y" or "y" or "N" or "n"
input from the str(raw_input)?
Thank''s in advance!
Snippet of code:
import os
def buildfinder():
os.system("CLS")
GameRoot = os.getenv("GAME_ROOT") + "\\"
print "Do you want to use " + GameRoot + " as your source
directory?"
usr = str(raw_input(''Y/N: ''))
return usr
#Runs the buildfinder function
usrinp = buildfinder()
def buildwhiler():
while usrinp != "y" or "Y" or "N" or "n": <<<<----PROBLEM
print "Enter Y or N!"
usr = str(raw_input(''Y/N: ''))
else:
code continues
推荐答案
正确的方式:
而usrinp!= " Y"或usrinp!=" Y"或usrinp!=" N"或usrinp!=" n":
必须在或两侧进行布尔评估。
或者在这种情况下:
而usrinp不在[''Y'','y'',''N'','n'']:
Ravi Teja。
Correct way:
while usrinp != "y" or usrinp != "Y" or usrinp != "N" or usrinp != "n":
There has to be a boolean evaluation on both sides of or.
Or in this case:
while usrinp not in [''Y'', ''y'', ''N'', ''n'']:
Ravi Teja.
是的,显然。
以上内容如下:
while(usrinp!=" y")或(" Y")或(" N")或(" n"):
你想要的是:
而usrinp!=" y"和usrinp!=" Y" \
和usrinp!=" n"和usrinp!=" N" :
哪个更好写成
而不是(usrinp ==" y"或usrinp ==" Y" \
或usrinp ==" n"或usrinp ==" N"):
可以使用str.lower进行简化:
而不是(usrinp.lower()==" y"或usrinp.lower()==" n"):
并再次简化,感谢Python''in ''运算符:
而不是usrinp.lower()inyn:
Yes, obviously.
The above reads as:
while (usrinp != "y") or ("Y") or ("N") or ("n"):
What you want is something like:
while usrinp != "y" and usrinp != "Y" \
and usrinp != "n" and usrinp != "N" :
which is better written as
while not (usrinp == "y" or usrinp == "Y" \
or usrinp == "n" or usrinp == "N") :
which can be simplified using str.lower:
while not (usrinp.lower() == "y" or usrinp.lower() == "n"):
and simplified again thanks to Python ''in'' operator:
while not usrinp.lower() in "yn":
虽然我们正在使用它,但代码中还有其他问题。其中一个是
如果usr首先键入
除了y,Y,之外的其他内容,则buildwhiler()将进入无限循环。 N'QUOT;或N。我让你试着找出原因,
然后给你两个提示:
- 全局变量不好(tm)
- don'将应用程序逻辑与用户界面逻辑混合在一起(即:一个函数
应该只处理一个或另一个,而不是混合两者)。
HTH
While we''re at it, there are other problems in your code. One of them is
that buildwhiler() will go in an infinite loop if the usr first typed
anything else than "y", "Y", "n" or "N". I let you try to find out why,
and just give you two hints :
- globals are Bad(tm)
- don''t mix application logic with user-interface logic (ie: a function
should only deal with one or the other, not mix both).
HTH
而userinp不在''yYnN'':
...
或者更一般地说:
而userinp.lower()不在''yn'':#案例独立测试
...
while userinp not in ''yYnN'':
...
Or maybe more generally:
while userinp.lower() not in ''yn'': # case independent test
...
这篇关于while while with“or”?请帮忙!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!