Hướng dẫn how do i take user input again and again in python? - làm cách nào để lấy dữ liệu người dùng nhập lại trong python?

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 lặp lại đầu vào trong Python?

Để lặp lại một chương trình cho đến khi đầu vào của người dùng là chính xác: Sử dụng vòng lặp trong thời gian để lặp lại cho đến khi đầu vào là chính xác.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 correct. 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 để tôi sử dụng đầu vào nhiều lần trong Python?

Python yêu cầu đầu vào của người dùng một lần nữa..
Trong ví dụ này, tôi đã lấy đầu vào dưới dạng AGE = INT (đầu vào (Nhập Enter Age: Hồi)) và vòng lặp trong khi.....
Trong khi đúng luôn đánh giá giá trị boolean đúng và thực hiện cơ thể của thời gian vô cực vòng lặp.....
Nếu điều kiện là đúng, nó sẽ trả về câu lệnh IF khác, nó trả về câu lệnh khác ..

Làm thế nào để bạn lấy đầu vào từ người dùng trong vòng lặp trong python?

Hàm input () được đảm bảo trả về một chuỗi, ngay cả khi người dùng nhập số nguyên ...
Khai báo một biến mới và khởi tạo nó vào một danh sách trống ..
Sử dụng lớp () để lặp n lần trong một vòng lặp ..
Trên mỗi lần lặp, hãy nối giá trị đầu vào vào danh sách ..