Hướng dẫn python repeat input if invalid - python lặp lại đầu vào nếu không hợp lệ

Phương pháp tiếp cận chức năng hoặc "Nhìn mẹ không có vòng lặp!":

from itertools import chain, repeat

prompts = chain(["Enter a number: "], repeat("Not a number! Try again: "))
replies = map(input, prompts)
valid_response = next(filter(str.isdigit, replies))
print(valid_response)
Enter a number:  a
Not a number! Try again:  b
Not a number! Try again:  1
1

Hoặc nếu bạn muốn có một thông báo "đầu vào xấu" được tách ra khỏi dấu nhắc đầu vào như trong các câu trả lời khác:

prompt_msg = "Enter a number: "
bad_input_msg = "Sorry, I didn't understand that."
prompts = chain([prompt_msg], repeat('\n'.join([bad_input_msg, prompt_msg])))
replies = map(input, prompts)
valid_response = next(filter(str.isdigit, replies))
print(valid_response)
Enter a number:  a
Sorry, I didn't understand that.
Enter a number:  b
Sorry, I didn't understand that.
Enter a number:  1
1

Làm thế nào nó hoạt động?

  1. prompts = chain(["Enter a number: "], repeat("Not a number! Try again: "))
    
    Sự kết hợp này của
    prompt_msg = "Enter a number: "
    bad_input_msg = "Sorry, I didn't understand that."
    prompts = chain([prompt_msg], repeat('\n'.join([bad_input_msg, prompt_msg])))
    replies = map(input, prompts)
    valid_response = next(filter(str.isdigit, replies))
    print(valid_response)
    
    6 và
    prompt_msg = "Enter a number: "
    bad_input_msg = "Sorry, I didn't understand that."
    prompts = chain([prompt_msg], repeat('\n'.join([bad_input_msg, prompt_msg])))
    replies = map(input, prompts)
    valid_response = next(filter(str.isdigit, replies))
    print(valid_response)
    
    7 sẽ tạo ra một trình lặp lại sẽ mang lại chuỗi
    prompt_msg = "Enter a number: "
    bad_input_msg = "Sorry, I didn't understand that."
    prompts = chain([prompt_msg], repeat('\n'.join([bad_input_msg, prompt_msg])))
    replies = map(input, prompts)
    valid_response = next(filter(str.isdigit, replies))
    print(valid_response)
    
    8 một lần và
    prompt_msg = "Enter a number: "
    bad_input_msg = "Sorry, I didn't understand that."
    prompts = chain([prompt_msg], repeat('\n'.join([bad_input_msg, prompt_msg])))
    replies = map(input, prompts)
    valid_response = next(filter(str.isdigit, replies))
    print(valid_response)
    
    9 Số lần vô hạn:
    for prompt in prompts:
        print(prompt)
    
    Enter a number: 
    Not a number! Try again: 
    Not a number! Try again: 
    Not a number! Try again: 
    # ... and so on
    
  2. Enter a number:  a
    Sorry, I didn't understand that.
    Enter a number:  b
    Sorry, I didn't understand that.
    Enter a number:  1
    1
    
    0 - Ở đây
    Enter a number:  a
    Sorry, I didn't understand that.
    Enter a number:  b
    Sorry, I didn't understand that.
    Enter a number:  1
    1
    
    1 sẽ áp dụng tất cả các chuỗi
    Enter a number:  a
    Sorry, I didn't understand that.
    Enter a number:  b
    Sorry, I didn't understand that.
    Enter a number:  1
    1
    
    2 từ bước trước vào hàm
    Enter a number:  a
    Sorry, I didn't understand that.
    Enter a number:  b
    Sorry, I didn't understand that.
    Enter a number:  1
    1
    
    3. Ví dụ:
    for reply in replies:
        print(reply)
    
    Enter a number:  a
    a
    Not a number! Try again:  1
    1
    Not a number! Try again:  it doesn't care now
    it doesn't care now
    # and so on...
    
  3. Chúng tôi sử dụng
    Enter a number:  a
    Sorry, I didn't understand that.
    Enter a number:  b
    Sorry, I didn't understand that.
    Enter a number:  1
    1
    
    4 và
    Enter a number:  a
    Sorry, I didn't understand that.
    Enter a number:  b
    Sorry, I didn't understand that.
    Enter a number:  1
    1
    
    5 để lọc các chuỗi chỉ chứa các chữ số:
    only_digits = filter(str.isdigit, replies)
    for reply in only_digits:
        print(reply)
    
    Enter a number:  a
    Not a number! Try again:  b
    Not a number! Try again:  1
    1
    
    0 và để chỉ nhận được chuỗi chỉ có chữ số đầu tiên mà chúng tôi sử dụng
    Enter a number:  a
    Sorry, I didn't understand that.
    Enter a number:  b
    Sorry, I didn't understand that.
    Enter a number:  1
    1
    
    6.

Các quy tắc xác nhận khác:

  1. Phương thức chuỗi: Tất nhiên bạn có thể sử dụng các phương thức chuỗi khác như

    Enter a number:  a
    Sorry, I didn't understand that.
    Enter a number:  b
    Sorry, I didn't understand that.
    Enter a number:  1
    1
    
    7 để chỉ nhận chuỗi chữ cái hoặc
    Enter a number:  a
    Sorry, I didn't understand that.
    Enter a number:  b
    Sorry, I didn't understand that.
    Enter a number:  1
    1
    
    8 để chỉ có chữ hoa. Xem tài liệu cho danh sách đầy đủ.
    Of course you can use other string methods like
    Enter a number:  a
    Sorry, I didn't understand that.
    Enter a number:  b
    Sorry, I didn't understand that.
    Enter a number:  1
    1
    
    7 to get only alphabetic strings, or
    Enter a number:  a
    Sorry, I didn't understand that.
    Enter a number:  b
    Sorry, I didn't understand that.
    Enter a number:  1
    1
    
    8 to get only uppercase. See docs for the full list.

  2. Thử nghiệm thành viên: Có một số cách khác nhau để thực hiện nó. Một trong số đó là bằng cách sử dụng phương thức

    Enter a number:  a
    Sorry, I didn't understand that.
    Enter a number:  b
    Sorry, I didn't understand that.
    Enter a number:  1
    1
    
    9:
    There are several different ways to perform it. One of them is by using
    Enter a number:  a
    Sorry, I didn't understand that.
    Enter a number:  b
    Sorry, I didn't understand that.
    Enter a number:  1
    1
    
    9 method:

    Enter a number:  a
    Not a number! Try again:  b
    Not a number! Try again:  1
    1
    
    1
    Enter a number:  a
    Not a number! Try again:  b
    Not a number! Try again:  1
    1
    
    2
  3. So sánh số: Có các phương pháp so sánh hữu ích mà chúng ta có thể sử dụng ở đây. Ví dụ: đối với

    prompts = chain(["Enter a number: "], repeat("Not a number! Try again: "))
    
    0 (
    prompts = chain(["Enter a number: "], repeat("Not a number! Try again: "))
    
    1):

    There are useful comparison methods which we can use here. For example, for
    prompts = chain(["Enter a number: "], repeat("Not a number! Try again: "))
    
    0 (
    prompts = chain(["Enter a number: "], repeat("Not a number! Try again: "))
    
    1):

    Enter a number:  a
    Not a number! Try again:  b
    Not a number! Try again:  1
    1
    
    3
    Enter a number:  a
    Not a number! Try again:  b
    Not a number! Try again:  1
    1
    
    4

    Hoặc, nếu bạn không thích sử dụng các phương thức Dunder (Dunder = Double-Underscore), bạn luôn có thể xác định chức năng của riêng mình hoặc sử dụng các phương thức từ mô-đun

    prompts = chain(["Enter a number: "], repeat("Not a number! Try again: "))
    
    2.

  4. Đường dẫn tồn tại: Ở đây người ta có thể sử dụng thư viện

    prompts = chain(["Enter a number: "], repeat("Not a number! Try again: "))
    
    3 và phương thức
    prompts = chain(["Enter a number: "], repeat("Not a number! Try again: "))
    
    4 của nó:

    Here one can use
    prompts = chain(["Enter a number: "], repeat("Not a number! Try again: "))
    
    3 library and its
    prompts = chain(["Enter a number: "], repeat("Not a number! Try again: "))
    
    4 method:

    Enter a number:  a
    Not a number! Try again:  b
    Not a number! Try again:  1
    1
    
    5
    Enter a number:  a
    Not a number! Try again:  b
    Not a number! Try again:  1
    1
    
    6

Giới hạn số lần thử:

Nếu bạn không muốn tra tấn người dùng bằng cách hỏi anh ta một số lần vô hạn, bạn có thể chỉ định giới hạn trong cuộc gọi là

prompt_msg = "Enter a number: "
bad_input_msg = "Sorry, I didn't understand that."
prompts = chain([prompt_msg], repeat('\n'.join([bad_input_msg, prompt_msg])))
replies = map(input, prompts)
valid_response = next(filter(str.isdigit, replies))
print(valid_response)
7. Điều này có thể được kết hợp với việc cung cấp giá trị mặc định cho hàm
Enter a number:  a
Sorry, I didn't understand that.
Enter a number:  b
Sorry, I didn't understand that.
Enter a number:  1
1
6:

Enter a number:  a
Not a number! Try again:  b
Not a number! Try again:  1
1
7
Enter a number:  a
Not a number! Try again:  b
Not a number! Try again:  1
1
8

Tiền xử lý dữ liệu đầu vào:

Đôi khi chúng tôi không muốn từ chối một đầu vào nếu người dùng vô tình cung cấp nó trong giới hạn hoặc với một khoảng trống ở đầu hoặc kết thúc của chuỗi. Để tính đến những sai lầm đơn giản này, chúng tôi có thể xử lý trước dữ liệu đầu vào bằng cách áp dụng các phương thức

prompts = chain(["Enter a number: "], repeat("Not a number! Try again: "))
7 và
prompts = chain(["Enter a number: "], repeat("Not a number! Try again: "))
8. Ví dụ, đối với trường hợp kiểm tra thành viên, mã sẽ trông như thế này:

Enter a number:  a
Not a number! Try again:  b
Not a number! Try again:  1
1
9
prompt_msg = "Enter a number: "
bad_input_msg = "Sorry, I didn't understand that."
prompts = chain([prompt_msg], repeat('\n'.join([bad_input_msg, prompt_msg])))
replies = map(input, prompts)
valid_response = next(filter(str.isdigit, replies))
print(valid_response)
0

Trong trường hợp khi bạn có nhiều chức năng để sử dụng để xử lý tiền xử lý, có thể dễ dàng sử dụng chức năng thực hiện thành phần chức năng. Ví dụ: sử dụng một từ đây:

prompt_msg = "Enter a number: "
bad_input_msg = "Sorry, I didn't understand that."
prompts = chain([prompt_msg], repeat('\n'.join([bad_input_msg, prompt_msg])))
replies = map(input, prompts)
valid_response = next(filter(str.isdigit, replies))
print(valid_response)
1
prompt_msg = "Enter a number: "
bad_input_msg = "Sorry, I didn't understand that."
prompts = chain([prompt_msg], repeat('\n'.join([bad_input_msg, prompt_msg])))
replies = map(input, prompts)
valid_response = next(filter(str.isdigit, replies))
print(valid_response)
2

Kết hợp các quy tắc xác nhận:

Ví dụ, đối với một trường hợp đơn giản, khi chương trình yêu cầu tuổi từ 1 đến 120, người ta chỉ có thể thêm một

Enter a number:  a
Sorry, I didn't understand that.
Enter a number:  b
Sorry, I didn't understand that.
Enter a number:  1
1
4 khác:

prompt_msg = "Enter a number: "
bad_input_msg = "Sorry, I didn't understand that."
prompts = chain([prompt_msg], repeat('\n'.join([bad_input_msg, prompt_msg])))
replies = map(input, prompts)
valid_response = next(filter(str.isdigit, replies))
print(valid_response)
3

Nhưng trong trường hợp khi có nhiều quy tắc, tốt hơn là thực hiện một chức năng thực hiện một kết hợp logic. Trong ví dụ sau, tôi sẽ sử dụng một cái sẵn sàng từ đây:

prompt_msg = "Enter a number: "
bad_input_msg = "Sorry, I didn't understand that."
prompts = chain([prompt_msg], repeat('\n'.join([bad_input_msg, prompt_msg])))
replies = map(input, prompts)
valid_response = next(filter(str.isdigit, replies))
print(valid_response)
4
prompt_msg = "Enter a number: "
bad_input_msg = "Sorry, I didn't understand that."
prompts = chain([prompt_msg], repeat('\n'.join([bad_input_msg, prompt_msg])))
replies = map(input, prompts)
valid_response = next(filter(str.isdigit, replies))
print(valid_response)
5

Thật không may, nếu ai đó cần một tin nhắn tùy chỉnh cho từng trường hợp thất bại, thì tôi sợ, không có cách nào khá chức năng. Hoặc, ít nhất, tôi không thể tìm thấy một.

Làm thế nào để bạn tiếp tục yêu cầu đầu vào cho đến khi hợp lệ trong Python?

Để tiếp tục yêu cầu đầu vào của người dùng cho đến khi phản hồi hợp lệ được đưa ra: sử dụng vòng lặp trong thời gian để lặp lại cho đến khi đầu vào hợp lệ. Trên mỗi lần lặp, kiểm tra xem đầu vào là một trong những giá trị dự kiến. Nếu điều kiện được đáp ứng, hãy sử dụng câu lệnh break để thoát ra khỏi vòng lặp.Use a while loop to iterate until the input is valid. On each iteration, check if the input is one of the expected values. If the condition is met, use the break statement to break out of the loop.

Làm thế nào để Python xử lý đầu vào không hợp lệ?

Yêu cầu người dùng nhập lại giá trị và một lần nữa nếu người dùng nhập giá trị không hợp lệ ...
Nhận đầu vào từ người dùng bằng raw_input () (python 2) hoặc input () (python 3) ..
Loại tên biến là chuỗi, vì vậy chúng tôi phải sử dụng phương thức chuỗi để nhập hợp lệ, nhập chuỗi ..
Sử dụng phương thức chuỗi isalpha () để kiểm tra chuỗi đã nhập của người dùng có hợp lệ hay không ..

Làm thế nào để bạn viết một vòng xác thực đầu vào trong Python?

Xác thực đầu vào trong vòng lặp trong một vòng trong Python #..
Sử dụng thử/ngoại trừ hoặc câu lệnh if/other để xác thực đầu vào ..
Nếu đầu vào không hợp lệ, hãy sử dụng câu lệnh tiếp tục để tiếp tục lặp tiếp theo ..
Nếu đầu vào là hợp lệ, hãy sử dụng câu lệnh break để thoát ra khỏi vòng lặp ..

Làm thế nào để bạn nhập một vòng lặp trong thời gian trong Python?

Để lấy đầu vào của người dùng trong vòng lặp thời gian: Sử dụng vòng lặp trong thời gian để lặp lại cho đến khi một điều kiện được đáp ứng. Sử dụng hàm input () để lấy đầu vào của người dùng.Nếu điều kiện được đáp ứng, thoát ra khỏi vòng lặp trong khi.Use a while loop to iterate until a condition is met. Use the input() function to take user input. If the condition is met, break out of the while loop.