Hướng dẫn read multiple lines from stdin python - đọc nhiều dòng từ stdin python

Có nhiều vấn đề riêng biệt với mã này:

while run:
    # loop through each line of user input, adding it to buffer
    for line in sys.stdin.readlines[]:
        if line == 'quit':
            run = False

Đầu tiên, bạn có một vòng lặp bên trong sẽ không hoàn thành cho đến khi tất cả các dòng đã được xử lý, ngay cả khi bạn gõ "bỏ" tại một số điểm. Cài đặt run = False không thoát ra khỏi vòng lặp đó. Thay vì bỏ thuốc ngay khi bạn gõ "bỏ", nó sẽ tiếp tục cho đến khi nó nhìn vào tất cả các dòng, và sau đó bỏ nếu bạn gõ "bỏ" bất cứ lúc nào.

Bạn có thể sửa lỗi này khá dễ dàng bằng cách thêm break sau run = False.

Nhưng, có hoặc không có bản sửa lỗi đó, nếu bạn không gõ "bỏ" trong lần đầu tiên qua vòng lặp bên ngoài, vì bạn đã đọc tất cả đầu vào, không có gì khác để đọc, vì vậy bạn sẽ tiếp tục chạy trống Vòng lặp bên trong lặp đi lặp lại mãi mãi mà bạn không bao giờ có thể thoát ra.

Bạn có một vòng lặp có nghĩa là "Đọc và xử lý tất cả các đầu vào". Bạn muốn làm điều đó chính xác một lần. Vì vậy, vòng lặp bên ngoài nên là gì? Nó không nên là dù sao; Cách để làm điều gì đó một lần là không sử dụng một vòng lặp. Vì vậy, để sửa chữa cái này, hãy loại bỏ run và vòng lặp ____99; Chỉ cần sử dụng vòng bên trong.

Sau đó, nếu bạn nhập "Thoát",

Copied!

lines = [] while True: user_input = input[] # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append[user_input + '\n'] # 👇️ prints list of strings print[lines] # 👇️ join list into a string print[''.join[lines]]
0 thực sự sẽ là

Copied!

lines = [] while True: user_input = input[] # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append[user_input + '\n'] # 👇️ prints list of strings print[lines] # 👇️ join list into a string print[''.join[lines]]
1, vì

Copied!

lines = [] while True: user_input = input[] # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append[user_input + '\n'] # 👇️ prints list of strings print[lines] # 👇️ join list into a string print[''.join[lines]]
2 không tước dòng mới.

Bạn sửa lỗi này bằng cách kiểm tra

Copied!

lines = [] while True: user_input = input[] # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append[user_input + '\n'] # 👇️ prints list of strings print[lines] # 👇️ join list into a string print[''.join[lines]]
1 hoặc

Copied!

lines = [] while True: user_input = input[] # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append[user_input + '\n'] # 👇️ prints list of strings print[lines] # 👇️ join list into a string print[''.join[lines]]
4Ping các dòng.

Cuối cùng, ngay cả khi bạn khắc phục tất cả những vấn đề này, bạn vẫn chờ đợi mãi mãi trước khi làm bất cứ điều gì.

Copied!

lines = [] while True: user_input = input[] # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append[user_input + '\n'] # 👇️ prints list of strings print[lines] # 👇️ join list into a string print[''.join[lines]]
2 Trả về

Copied!

lines = [] while True: user_input = input[] # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append[user_input + '\n'] # 👇️ prints list of strings print[lines] # 👇️ join list into a string print[''.join[lines]]
6 của các dòng. Cách duy nhất có thể làm điều đó là bằng cách đọc tất cả các dòng sẽ có trên

Copied!

lines = [] while True: user_input = input[] # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append[user_input + '\n'] # 👇️ prints list of strings print[lines] # 👇️ join list into a string print[''.join[lines]]
7. Bạn thậm chí không thể bắt đầu lặp lại cho đến khi bạn đọc tất cả các dòng đó.

Khi đầu vào tiêu chuẩn là một tệp, điều đó xảy ra khi tệp kết thúc, vì vậy nó không quá khủng khiếp. Nhưng khi đầu vào tiêu chuẩn là dấu nhắc lệnh windows, dấu nhắc lệnh không bao giờ kết thúc.* Vì vậy, điều này mất mãi mãi. Bạn không thể bắt đầu xử lý danh sách các dòng, bởi vì phải chờ đợi mãi mãi để chờ đợi danh sách các dòng.

Giải pháp là không sử dụng

Copied!

lines = [] while True: user_input = input[] # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append[user_input + '\n'] # 👇️ prints list of strings print[lines] # 👇️ join list into a string print[''.join[lines]]
8. Thực sự, không bao giờ có một lý do chính đáng để gọi

Copied!

lines = [] while True: user_input = input[] # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append[user_input + '\n'] # 👇️ prints list of strings print[lines] # 👇️ join list into a string print[''.join[lines]]
8 trên bất cứ điều gì,

Copied!

lines = [] while True: user_input = input[] # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append[user_input + '\n'] # 👇️ prints list of strings print[lines] # 👇️ join list into a string print[''.join[lines]]
7 hay không. Bất cứ điều gì

Copied!

lines = [] while True: user_input = input[] # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append[user_input + '\n'] # 👇️ prints list of strings print[lines] # 👇️ join list into a string print[''.join[lines]]
2 hoạt động đều đã có một số dòng đầy đủ, giống như

Copied!

lines = [] while True: user_input = input[] # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append[user_input + '\n'] # 👇️ prints list of strings print[lines] # 👇️ join list into a string print[''.join[lines]]
6 mà

Copied!

lines = [] while True: user_input = input[] # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append[user_input + '\n'] # 👇️ prints list of strings print[lines] # 👇️ join list into a string print[''.join[lines]]
2 sẽ cung cấp cho bạn, ngoại trừ nó là "lười biếng": nó có thể cung cấp cho bạn các dòng một lúc một lần. [Và ngay cả khi bạn thực sự cần danh sách, chỉ cần làm

Copied!

import sys # 👇️ multiline input using stdin.readlines[] # 👇️ User must press Ctrl + D [Unix] or Ctrl + Z to exit print['Press CTRL + D [Unix] or CTRL + Z [Windows] to exit'] user_input = sys.stdin.readlines[] # 👇️ get list of lines print[user_input] # 👇️ join the list items into a string print[''.join[user_input]]
4 thay vì

Copied!

import sys # 👇️ multiline input using stdin.readlines[] # 👇️ User must press Ctrl + D [Unix] or Ctrl + Z to exit print['Press CTRL + D [Unix] or CTRL + Z [Windows] to exit'] user_input = sys.stdin.readlines[] # 👇️ get list of lines print[user_input] # 👇️ join the list items into a string print[''.join[user_input]]
5.]

Vì vậy, thay vì

Copied!

import sys # 👇️ multiline input using stdin.readlines[] # 👇️ User must press Ctrl + D [Unix] or Ctrl + Z to exit print['Press CTRL + D [Unix] or CTRL + Z [Windows] to exit'] user_input = sys.stdin.readlines[] # 👇️ get list of lines print[user_input] # 👇️ join the list items into a string print[''.join[user_input]]
6, chỉ cần thực hiện

Copied!

import sys # 👇️ multiline input using stdin.readlines[] # 👇️ User must press Ctrl + D [Unix] or Ctrl + Z to exit print['Press CTRL + D [Unix] or CTRL + Z [Windows] to exit'] user_input = sys.stdin.readlines[] # 👇️ get list of lines print[user_input] # 👇️ join the list items into a string print[''.join[user_input]]
7 [hoặc, tốt hơn, thay thế hoàn toàn vòng lặp rõ ràng và sử dụng chuỗi các phép biến đổi lặp, như trong câu trả lời của Mgilson.]

Các bản sửa lỗi Jbernardo, Wing Tang Wong, vv được đề xuất đều chính xác và cần thiết. Lý do không ai trong số họ khắc phục sự cố của bạn là nếu bạn có 4 lỗi và sửa 1, mã của bạn vẫn không hoạt động. Đó chính xác là lý do tại sao "không hoạt động" không phải là thước đo hữu ích của bất cứ điều gì trong lập trình và bạn phải gỡ lỗi những gì thực sự sai khi biết liệu bạn có đang tiến bộ hay không.

* Tôi đã nói dối một chút về

Copied!

lines = [] while True: user_input = input[] # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append[user_input + '\n'] # 👇️ prints list of strings print[lines] # 👇️ join list into a string print[''.join[lines]]
7 không bao giờ được hoàn thành. Nếu bạn gõ A Control-Z [bạn có thể hoặc không cần phải theo dõi nó bằng một bản trả lại], thì

Copied!

lines = [] while True: user_input = input[] # 👇️ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append[user_input + '\n'] # 👇️ prints list of strings print[lines] # 👇️ join list into a string print[''.join[lines]]
7 đã hoàn thành. Nhưng nếu nhiệm vụ của bạn là thực hiện thoát ngay khi người dùng gõ "bỏ"

Bài Viết Liên Quan

Chủ Đề