Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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
While循环中断问题(Python)_Python_Loops_While Loop - Fatal编程技术网

While循环中断问题(Python)

While循环中断问题(Python),python,loops,while-loop,Python,Loops,While Loop,我目前正在尝试连接到GPS蓝牙设备。我的Python2.7代码最初运行正常,但我现在尝试将代码实现为while循环,这样,当我的设备不可用时,它将继续为它循环。不幸的是,我的代码似乎陷入了一个循环,并反复打印出错误消息“无法定位蓝牙GPS设备。重试…”我正在使用PyBluez的蓝牙模块 这是我的密码:- import bluetooth target_address = "00:11:22:33:44:55:66" discovered_devices = discover_devices(

我目前正在尝试连接到GPS蓝牙设备。我的Python2.7代码最初运行正常,但我现在尝试将代码实现为while循环,这样,当我的设备不可用时,它将继续为它循环。不幸的是,我的代码似乎陷入了一个循环,并反复打印出错误消息“无法定位蓝牙GPS设备。重试…”我正在使用PyBluez的蓝牙模块

这是我的密码:-

import bluetooth

target_address = "00:11:22:33:44:55:66"

discovered_devices = discover_devices()  # Object to discover devices from Bluetooth module

while True:
    print "Attempting to locate the correct Bluetooth GPS Device..."
    for address in discovered_devices:
        if address != target_address:
            print "Unable to Locate Bluetooth GPS Device. Retrying..."
        else:
            print "Bluetooth GPS Device Located: ", target_address
            break

# move on to next statement outside of loop (connection etc...)
如前所述,基本上我希望实现的是设备发现对象启动,控制台上会出现一条消息,指示它正在寻找传输指定设备地址的设备(即“00:11:22:33:44:55:66”)。如果没有设备具有此地址,我希望代码显示与无法定位设备相关的错误消息,然后我希望它继续查找

另一方面,我也希望最终编辑此代码,以便在多次尝试定位设备X时间/但没有成功后,我希望代码结束,程序显示错误消息。有什么指导吗


谢谢

您打破了循环的
,而不是外部的
while
循环。如果在
for
循环之后没有执行任何操作,可以通过添加以下内容来传播
中断

while True:
    print "Attempting to locate the correct Bluetooth GPS Device..."
    for address in discovered_devices:
        if address != target_address:
            print "Unable to Locate Bluetooth GPS Device. Retrying..."
        else:
            print "Bluetooth GPS Device Located: ", target_address
            break

您正在中断
for
循环,而不是外部
while
循环。如果在
for
循环之后没有执行任何操作,可以通过添加以下内容来传播
中断

while True:
    print "Attempting to locate the correct Bluetooth GPS Device..."
    for address in discovered_devices:
        if address != target_address:
            print "Unable to Locate Bluetooth GPS Device. Retrying..."
        else:
            print "Bluetooth GPS Device Located: ", target_address
            break
线路

discovered_devices = discover_devices()
在进入
for
循环之前,应该进入
while
循环

然后将
while
循环替换为
for
循环,以限制尝试次数

要正确退出
for
循环的内部
,请按照@Jeremy所说的操作:添加

else:
    continue
break
最后

在外循环的每次迭代中,您可能还希望在每次尝试之间使用
sleep()
等待

discovered_devices = discover_devices()
在进入
for
循环之前,应该进入
while
循环

然后将
while
循环替换为
for
循环,以限制尝试次数

要正确退出
for
循环的内部
,请按照@Jeremy所说的操作:添加

else:
    continue
break
最后


在外部循环的每次迭代中,您可能还希望在每次尝试之间使用
sleep()
等待。

如果您在
循环中执行任何类型的睡眠来减慢
,请确保在
continue
之前的
else
子句中执行,而不是在其下方。另外,我认为Simon是对的——设备发现应该在
while
循环的顶部,否则每次迭代都是一样的。如果你正在做任何睡眠来减慢
while
循环,请确保在
continue
之前的
else
子句中进行,而不是在它下面。另外,我认为Simon是对的——设备发现应该在
while
循环的顶部,否则每次迭代都是一样的。