Python 如何激活while true语句

Python 如何激活while true语句,python,function,input,while-loop,Python,Function,Input,While Loop,所以我想在我的代码中添加一个while True语句,这样当一个变量为false时,它就不起作用了,但当该变量更改为True时,它就会激活。在我的情况下,我有一个开放式简历摄像头,它扫描二维码,并想知道如何激活它,因为它使用了while true语句。任何想法。下面的例子 hello = false r = input("hello or hi: ") if r == 'hi': hello = True while hello = True: _, img

所以我想在我的代码中添加一个while True语句,这样当一个变量为false时,它就不起作用了,但当该变量更改为True时,它就会激活。在我的情况下,我有一个开放式简历摄像头,它扫描二维码,并想知道如何激活它,因为它使用了while true语句。任何想法。下面的例子

hello = false
r = input("hello or hi: ")
if r == 'hi':
hello = True
     while hello = True:
    _, img = cap.read()
    data, bbox, _ = detector.detectAndDecode(img)
    if bbox is not None:
        for i in range(len(bbox)):
            cv2.line(img, tuple(bbox[i][0]), tuple(bbox[(i + 1) % len(bbox)][0]), color=(255, 0, 0), thickness=2)

鉴于您修复了缩进,并在需要时使用break语句停止循环,上述操作也应该可以正常工作

现在应该根据您的需要实施中断。无论何时调用break,循环都将终止,因此您可以在if语句中实现它,或者在循环结束时实现它,具体取决于您的使用情况

还可以使用if语句检查变量是否为true,然后才运行while循环。当您想在while语句中使用不同的条件时,它会很有用

hello = false
r = input("hello or hi: ")
if r == 'hi':
    hello = True
if hello:
    j = 1
    while j < 5:
        _, img = cap.read()
        data, bbox, _ = detector.detectAndDecode(img)
        if bbox is not None:
            for i in range(len(bbox)):
                cv2.line(img, tuple(bbox[i][0]), tuple(bbox[(i + 1) % len(bbox)][0]), color=(255, 0, 0), thickness=2)
        j += 1
hello=false
r=输入(“你好或你好:”)
如果r=='hi':
你好=真的
如果您好:
j=1
当j<5时:
_,img=cap.read()
数据,bbox,ux=检测器。检测和解码(img)
如果bbox不是None:
对于范围内的i(len(bbox)):
cv2.直线(img,元组(bbox[i][0]),元组(bbox[(i+1)%len(bbox)][0]),颜色=(255,0,0),厚度=2)
j+=1
在上面的代码中,while循环仅在hello变量为True时执行,然后while循环将继续迭代,直到j的值等于或大于5。您应该根据程序中的要求实现这些事情,这是两种实现方法

hello = false
r = input("hello or hi: ")
if r == 'hi':
    hello = True
if hello:
    j = 1
    while j < 5:
        _, img = cap.read()
        data, bbox, _ = detector.detectAndDecode(img)
        if bbox is not None:
            for i in range(len(bbox)):
                cv2.line(img, tuple(bbox[i][0]), tuple(bbox[(i + 1) % len(bbox)][0]), color=(255, 0, 0), thickness=2)
        j += 1