Có/không trong khi vòng lặp với đầu vào của người dùng trong python #
Để tạo số có/không trong khi vòng lặp với đầu vào của người dùng:
- Sử dụng vòng lặp
while
để lặp lại cho đến khi một điều kiện được đáp ứng. - Sử dụng chức năng
input[]
để nhận đầu vào từ người dùng. - Nếu người dùng loại
no
, hãy sử dụng câu lệnhbreak
để thoát ra khỏi vòng lặp.
Copied!
user_input = '' while True: user_input = input['Do you want to continue? yes/no: '] if user_input.lower[] == 'yes': print['User typed yes'] continue elif user_input.lower[] == 'no': print['User typed no'] break else: print['Type yes/no']
Chúng tôi đã sử dụng vòng lặp while True
để lặp lại cho đến khi người dùng loại no
.
Câu lệnh Copied! Copied!
1 kiểm tra xem người dùng đã nhập print['YES'.lower[]] # 👉️ 'yes'
print['Yes'.lower[]] # 👉️ 'yes'
2 và nếu điều kiện được đáp ứng, nó sẽ tiếp tục lặp lại tiếp theo.print['YES'.lower[]] # 👉️ 'yes'
print['Yes'.lower[]] # 👉️ 'yes'
Chúng tôi đã sử dụng phương thức
3 để chuyển đổi chuỗi đầu vào của người dùng thành chữ thường để thực hiện so sánh bình đẳng không phân biệt chữ hoa chữ thường.Copied!
print['YES'.lower[]] # 👉️ 'yes' print['Yes'.lower[]] # 👉️ 'yes'
Copied!
print['YES'.lower[]] # 👉️ 'yes' print['Yes'.lower[]] # 👉️ 'yes'
Phương thức Str.Lower trả về một bản sao của chuỗi với tất cả các ký tự được chuyển đổi thành chữ thường.
Tuyên bố
4 tiếp tục với lần lặp tiếp theo của vòng lặp.Copied!
print['YES'.lower[]] # 👉️ 'yes' print['Yes'.lower[]] # 👉️ 'yes'
Copied!
user_input = '' while True: user_input = input['Do you want to continue? yes/no: '] if user_input.lower[] == 'yes': print['User typed yes'] continue elif user_input.lower[] == 'no': print['User typed no'] break else: print['Type yes/no']
Nếu người dùng loại no
, chúng tôi in một tin nhắn và thoát ra khỏi vòng lặp while True
.
Tuyên bố phá vỡ thoát ra khỏi vòng lặp
7 hoặcCopied!
print['YES'.lower[]] # 👉️ 'yes' print['Yes'.lower[]] # 👉️ 'yes'
while
.Khối
9 chạy khi người dùng gõ bất cứ thứ gì khác.Copied!
print['YES'.lower[]] # 👉️ 'yes' print['Yes'.lower[]] # 👉️ 'yes'
Bạn cũng có thể có nhiều từ mà bạn coi là
2 hoặcCopied!
print['YES'.lower[]] # 👉️ 'yes' print['Yes'.lower[]] # 👉️ 'yes'
no
.Nếu đó là trường hợp, hãy thêm các từ vào danh sách và sử dụng toán tử
2 để kiểm tra tư cách thành viên.Copied!
user_input = '' while True: user_input = input['Do you want to continue? yes/no: '] if user_input.lower[] == 'yes': print['User typed yes'] continue elif user_input.lower[] == 'no': print['User typed no'] break else: print['Type yes/no']
Copied!
yes_choices = ['yes', 'y'] no_choices = ['no', 'n'] while True: user_input = input['Do you want to continue? yes/no: '] if user_input.lower[] in yes_choices: print['User typed yes'] continue elif user_input.lower[] in no_choices: print['User typed no'] break else: print['Type yes/no']
Chúng tôi đã sử dụng toán tử
2 để kiểm tra xem giá trị đầu vào có phải là một trong những mục trong danh sách không.Copied!
user_input = '' while True: user_input = input['Do you want to continue? yes/no: '] if user_input.lower[] == 'yes': print['User typed yes'] continue elif user_input.lower[] == 'no': print['User typed no'] break else: print['Type yes/no']
Các thử nghiệm trong nhà điều hành để thành viên. Ví dụ,
4 đánh giá thànhCopied!
user_input = '' while True: user_input = input['Do you want to continue? yes/no: '] if user_input.lower[] == 'yes': print['User typed yes'] continue elif user_input.lower[] == 'no': print['User typed no'] break else: print['Type yes/no']
5 nếuCopied!
user_input = '' while True: user_input = input['Do you want to continue? yes/no: '] if user_input.lower[] == 'yes': print['User typed yes'] continue elif user_input.lower[] == 'no': print['User typed no'] break else: print['Type yes/no']
6 là thành viên củaCopied!
user_input = '' while True: user_input = input['Do you want to continue? yes/no: '] if user_input.lower[] == 'yes': print['User typed yes'] continue elif user_input.lower[] == 'no': print['User typed no'] break else: print['Type yes/no']
7, nếu không nó sẽ đánh giá thànhCopied!
user_input = '' while True: user_input = input['Do you want to continue? yes/no: '] if user_input.lower[] == 'yes': print['User typed yes'] continue elif user_input.lower[] == 'no': print['User typed no'] break else: print['Type yes/no']
8.Copied!
user_input = '' while True: user_input = input['Do you want to continue? yes/no: '] if user_input.lower[] == 'yes': print['User typed yes'] continue elif user_input.lower[] == 'no': print['User typed no'] break else: print['Type yes/no']
Bạn cũng có thể sử dụng vòng lặp while
nếu bạn chỉ muốn cho phép người dùng nhập một số biến thể của
2 vàCopied!
print['YES'.lower[]] # 👉️ 'yes' print['Yes'.lower[]] # 👉️ 'yes'
no
.Copied!
yes_choices = ['yes', 'y'] no_choices = ['no', 'n'] while True: user_input = input['Do you like pizza [yes/no]: '] if user_input.lower[] in yes_choices: print['user typed yes'] break elif user_input.lower[] in no_choices: print['user typed no'] break else: print['Type yes or no'] continue
Chúng tôi đã sử dụng vòng lặp while
để chỉ cho phép người dùng trả lời
2,Copied!
print['YES'.lower[]] # 👉️ 'yes' print['Yes'.lower[]] # 👉️ 'yes'
4,Copied!
yes_choices = ['yes', 'y'] no_choices = ['no', 'n'] while True: user_input = input['Do you want to continue? yes/no: '] if user_input.lower[] in yes_choices: print['User typed yes'] continue elif user_input.lower[] in no_choices: print['User typed no'] break else: print['Type yes/no']
no
hoặc 6.Copied!
yes_choices = ['yes', 'y'] no_choices = ['no', 'n'] while True: user_input = input['Do you want to continue? yes/no: '] if user_input.lower[] in yes_choices: print['User typed yes'] continue elif user_input.lower[] in no_choices: print['User typed no'] break else: print['Type yes/no']
Nếu khối
1 chạy, chúng tôi in một tin nhắn và sử dụng câu lệnhCopied!
print['YES'.lower[]] # 👉️ 'yes' print['Yes'.lower[]] # 👉️ 'yes'
break
để thoát khỏi vòng lặp.Tuyên bố phá vỡ thoát ra khỏi vòng lặp
7 hoặcCopied!
print['YES'.lower[]] # 👉️ 'yes' print['Yes'.lower[]] # 👉️ 'yes'
while
.Khối
9 chạy khi người dùng gõ bất cứ thứ gì khác.Copied!
print['YES'.lower[]] # 👉️ 'yes' print['Yes'.lower[]] # 👉️ 'yes'