簡體   English   中英

如何在以下代碼中為每個while循環取不同的r值?

[英]how I can take a different value of r for each while loop in the following code?python

import random
class check_error:
def __init__(self,firstbyte=bytearray(b'\x15\x04\xA5')) :
    r=random.choice (firstbyte)
    self.r=r

def pocket_data(self):


    print("I am sending request for NBP/SpO2 datas")
    r=self.r

    while True:    
        print("blala")
        r=self.r
        print(r)

        try: 
            if int("{:02x}".format(r))==15:
                print("Negative Acknowledgment.Error occured during data transmission to device.I am sending the datas again... ")
                continue

            elif r==4:
                print("Host does not have the capability to respond to the request,it only supports a subset of the protocol")
                continue

        except:
            print("done")
            break   

s=check_error()
print(s.pocket_data())

如果選擇的元素是x15或x04,循環將變得無窮無盡,但是我想做的是選擇字節數組的另一個元素,直到選擇的元素是xA5。

初始化類時, r設置為random.choice (firstbyte) 注意,這是永遠不會改變的。 設置r的值,然后分配self.r = r ,然后就不再更改self.r

如果希望r繼續滾動隨機數, firstbyte是將firstbyte保存到類中的變量中,例如:

def __init__(self,firstbyte=bytearray(b'\x15\x04\xA5')) :
    self.firstbyte = firstbyte
    r=random.choice (firstbyte)
    self.r=r

然后,在循環內,繼續以與構造函數相同的方式生成隨機數:

while True:    
    self.r = random.choice(self.firstbyte)
    ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM