4
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Khi đặt trở lại trong khi vòng lặp, vòng lặp sẽ dừng làm thế nào để sửa nó?
ser = serial.Serial[
port='COM5',
baudrate = 9600,
timeout=1]
while 1:
x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
print[x]
Liệu bạn có thể giúp mình không?
Đã hỏi ngày 13 tháng 1 năm 2017 lúc 11:03Jan 13, 2017 at 11:03
9
Đơn giản chỉ cần lấy của bạn
x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
Đặt nó vào một chức năng như
def foo[ser]:
x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x]
và thay đổi vòng lặp trong khi của bạn thành đơn giản là
while 1:
print[foo[ser]]
Tuy nhiên @Developius đã có một giải pháp tốt hơn trông giống như
while 1:
x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
print[x]
Đã trả lời ngày 13 tháng 1 năm 2017 lúc 11:20Jan 13, 2017 at 11:20
DillanmdillanmDillanm
88612 Huy hiệu bạc28 Huy hiệu đồng12 silver badges28 bronze badges
2
Nếu bạn muốn chuyển các giá trị liên tục đến một đoạn mã riêng biệt, bạn có thể sử dụng
x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
4.Bạn có thể sửa đổi mã của mình như sau:
def func[]:
while 1:
x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
yield x
Và sau đó gọi chức năng. Lưu ý rằng F sẽ là một trình tạo nên bạn sẽ phải lặp qua nó như dưới đây.
f = func[]
for i in f:
print[i] # or use i for any purpose
Tham khảo: Ở đây
Đã trả lời ngày 14 tháng 2 năm 2021 lúc 8:04Feb 14, 2021 at 8:04
asifasifasif
Phù hiệu bằng đồng 11644 bronze badges
1
bạn có thể thử cái này
while 1:
x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return x
Đã trả lời ngày 13 tháng 1 năm 2017 lúc 11:43Jan 13, 2017 at 11:43
x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
5 và x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
6 cho phép bạn kiểm soát luồng của các vòng lặp. Họ là một khái niệm mà người mới bắt đầu Python có xu hướng hiểu lầm, vì vậy hãy chú ý cẩn thận.Sử dụng x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
5
x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
Tuyên bố
x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
5 sẽ hoàn toàn thoát ra khỏi vòng lặp hiện tại, có nghĩa là nó đã giành được nhiều câu lệnh có thêm bên trong nó.>>> names = ["Rose", "Max", "Nina", "Phillip"]
>>> for name in names:
... print[f"Hello, {name}"]
... if name == "Nina":
... break
...
Hello, Rose
Hello, Max
Hello, Nina
x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
5 hoàn toàn thoát ra khỏi vòng lặp.breaks out of the loop.Sử dụng x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
6
x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
6 hoạt động hơi khác một chút. Thay vào đó, nó quay trở lại bắt đầu vòng lặp, bỏ qua bất kỳ câu lệnh nào khác trong vòng lặp.>>> for name in names:
... if name != "Nina":
... continue
... print[f"Hello, {name}"]
...
Hello, Nina
x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
6 tiếp tục bắt đầu vòng lặpstart of the loopx=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
5 và x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
6 được hiển thị
x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
Điều gì xảy ra khi chúng ta chạy mã từ tệp Python này?
x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
0Kết quả
Xem nếu bạn có thể đoán kết quả trước khi mở rộng phần này.
Sử dụng x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
5 và x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
6 trong các vòng lồng nhau.
x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
Hãy nhớ rằng,
x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
5 và x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
6 chỉ hoạt động cho vòng lặp hiện tại. Mặc dù tôi đã lập trình Python trong nhiều năm, nhưng đây là điều vẫn còn vận chuyển tôi!current
loop. Even though I’ve been programming Python for years, this is something that still trips me up!x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
1x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
5 trong vòng bên trong chỉ bị phá vỡ trong vòng lặp bên trong! Vòng ngoài tiếp tục chạy.Kiểm soát vòng lặp trong các vòng while 1:
print[foo[ser]]
0
while 1:
print[foo[ser]]
Bạn cũng có thể sử dụng
x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
5 và x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
6 trong các vòng while 1:
print[foo[ser]]
0. Một kịch bản phổ biến là chạy một vòng lặp mãi mãi, cho đến khi một điều kiện nhất định được đáp ứng.x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
2Hãy cẩn thận rằng tình trạng của bạn cuối cùng sẽ được đáp ứng, nếu không thì chương trình của bạn sẽ bị mắc kẹt trong một vòng lặp vô hạn. Để sử dụng sản xuất, nó tốt hơn để sử dụng lập trình không đồng bộ.
Các vòng lặp và câu lệnh while 1:
print[foo[ser]]
4
while 1:
print[foo[ser]]
Giống như trong các chức năng, hãy xem xét tuyên bố
while 1:
print[foo[ser]]
4 là chuyển đổi cứng của vòng lặp.x=str[ser.readline[]]
x = re.findall["\d+\.\d+", x]
x = float[x[0]]
return[x] #loop stopped
3