![](/img/trans.png)
[英]How can I count each dictionary value output in Python in the following code example?
[英]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.