Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用;或;及;不是";在“a”中;而";循环python 3_Python_Python 3.x - Fatal编程技术网

如何使用;或;及;不是";在“a”中;而";循环python 3

如何使用;或;及;不是";在“a”中;而";循环python 3,python,python-3.x,Python,Python 3.x,我正在尝试将while与not和或一起使用,但由于某些原因,我的代码无法工作 orientation = "" while orientation is not "h" or "v": print("Type 'h' for horizontal and 'v' for vertical") orientation = input() if orientation == "h": do_something() if orientation ==

我正在尝试将
while
not
一起使用,但由于某些原因,我的代码无法工作

orientation = ""
while orientation is not "h" or "v":
    print("Type 'h' for horizontal and 'v' for vertical") 
    orientation = input()
    if orientation == "h":
        do_something()
    if orientation == "v":
        do_something()

预期的结果是,如果我在输入中键入“h”或“v”,将调用
do_something()
,while循环将结束,但相反,while循环将继续并重复。我做错了什么?

写这篇文章的一种方式是:

while orientation not in {"h", "v"}:
或者,由于您已经在检查循环中的
“h”
“v”
,您可以避免重复自己的操作:

while True:
    print("Type 'h' for horizontal and 'v' for vertical") 
    orientation = input()
    if orientation == "h":
        do_something()
        break
    if orientation == "v":
        do_something()
        break

(可能将第二个
if
更改为
elif
,并选择性地添加一个
else
子句,告知用户他们的输入未被识别)。

一种编写方法如下:

while orientation not in {"h", "v"}:
或者,由于您已经在检查循环中的
“h”
“v”
,您可以避免重复自己的操作:

while True:
    print("Type 'h' for horizontal and 'v' for vertical") 
    orientation = input()
    if orientation == "h":
        do_something()
        break
    if orientation == "v":
        do_something()
        break

(可能将第二个
if
更改为
elif
,并可选择添加
else
子句,告知用户其输入未被识别)。

使用
方向不在['h','v']
使用
方向不在['h','v']