Có/không có câu hỏi với đầu vào của người dùng trong python #
Để hỏi người dùng một câu hỏi có/không:
- Sử dụng chức năng
input[]
để lấy đầu vào từ người dùng. - Sử dụng các câu lệnh có điều kiện để kiểm tra xem người dùng đã nhập
yes
hoặcno
. - Thực hiện một hành động nếu một trong hai điều kiện được đáp ứng.
Copied!
user_input = input['Do you like pizza [yes/no]: '] if user_input.lower[] == 'yes': print['user typed yes'] elif user_input.lower[] == 'no': print['user typed no'] else: print['Type yes or no']
Chúng tôi đã sử dụng chức năng input[]
để lấy đầu vào từ người dùng.
Tuyên bố if
kiểm tra xem người dùng đã nhập yes
và in tin nhắn.
Chúng tôi đã sử dụng phương thức
0 để 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.
Khối
1 chạy nếu người dùng gõ một cái 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à yes
hoặc 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ử
4 để kiểm tra tư cách thành viên.Copied!
print['YES'.lower[]] # 👉️ 'yes' print['Yes'.lower[]] # 👉️ 'yes'
Copied!
user_input = input['Do you like pizza [yes/no]: '] yes_choices = ['yes', 'y'] no_choices = ['no', 'n'] if user_input.lower[] in yes_choices: print['user typed yes'] elif user_input.lower[] in no_choices: print['user typed no'] else: print['Type yes or no']
Chúng tôi đã sử dụng toán tử
4 để 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!
print['YES'.lower[]] # 👉️ 'yes' print['Yes'.lower[]] # 👉️ 'yes'
Các thử nghiệm trong nhà điều hành để thành viên. Ví dụ,
6 đánh giá thànhCopied!
print['YES'.lower[]] # 👉️ 'yes' print['Yes'.lower[]] # 👉️ 'yes'
7 nếuCopied!
print['YES'.lower[]] # 👉️ 'yes' print['Yes'.lower[]] # 👉️ 'yes'
8 là thành viên củaCopied!
print['YES'.lower[]] # 👉️ 'yes' print['Yes'.lower[]] # 👉️ 'yes'
9, nếu không nó sẽ đánh giá thànhCopied!
print['YES'.lower[]] # 👉️ 'yes' print['Yes'.lower[]] # 👉️ 'yes'
0.Copied!
user_input = input['Do you like pizza [yes/no]: '] yes_choices = ['yes', 'y'] no_choices = ['no', 'n'] if user_input.lower[] in yes_choices: print['user typed yes'] elif user_input.lower[] in no_choices: print['user typed no'] else: print['Type yes or no']
Nếu bạn chỉ muốn cho phép người dùng nhập một số biến thể của yes
và no
, hãy sử dụng vòng lặp
3.Copied!
user_input = input['Do you like pizza [yes/no]: '] yes_choices = ['yes', 'y'] no_choices = ['no', 'n'] if user_input.lower[] in yes_choices: print['user typed yes'] elif user_input.lower[] in no_choices: print['user typed no'] else: print['Type yes or 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
3 để chỉ cho phép người dùng trả lờiCopied!
user_input = input['Do you like pizza [yes/no]: '] yes_choices = ['yes', 'y'] no_choices = ['no', 'n'] if user_input.lower[] in yes_choices: print['user typed yes'] elif user_input.lower[] in no_choices: print['user typed no'] else: print['Type yes or no']
yes
, 6,Copied!
user_input = input['Do you like pizza [yes/no]: '] yes_choices = ['yes', 'y'] no_choices = ['no', 'n'] if user_input.lower[] in yes_choices: print['user typed yes'] elif user_input.lower[] in no_choices: print['user typed no'] else: print['Type yes or no']
no
hoặc 8.Copied!
user_input = input['Do you like pizza [yes/no]: '] yes_choices = ['yes', 'y'] no_choices = ['no', 'n'] if user_input.lower[] in yes_choices: print['user typed yes'] elif user_input.lower[] in no_choices: print['user typed no'] else: print['Type yes or no']
Nếu khối if
chạy, chúng tôi in một tin nhắn và sử dụng câu lệnh
0 để thoát khỏi vòng lặp.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
Tuyên bố phá vỡ thoát ra khỏi vòng lặp
1 hoặcCopied!
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
3.Copied!
user_input = input['Do you like pizza [yes/no]: '] yes_choices = ['yes', 'y'] no_choices = ['no', 'n'] if user_input.lower[] in yes_choices: print['user typed yes'] elif user_input.lower[] in no_choices: print['user typed no'] else: print['Type yes or no']
Nếu người dùng nhập giá trị không hợp lệ, khối Copied! Copied!
1 sẽ chạy, trong đó chúng tôi sử dụng câu lệnh print['YES'.lower[]] # 👉️ 'yes'
print['Yes'.lower[]] # 👉️ 'yes'
4 để nhắc lại người dùng.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
Tuyên bố
4 tiếp tục với lần lặp tiếp theo của vòng lặp.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
Khi xác thực đầu vào của người dùng trong vòng lặp
3, chúng tôi sử dụng câu lệnhCopied!
user_input = input['Do you like pizza [yes/no]: '] yes_choices = ['yes', 'y'] no_choices = ['no', 'n'] if user_input.lower[] in yes_choices: print['user typed yes'] elif user_input.lower[] in no_choices: print['user typed no'] else: print['Type yes or no']
4 khi đầu vào không hợp lệ.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
Nếu đầu vào là hợp lệ, chúng tôi sử dụng câu lệnh
0 để thoát khỏi vòng lặpCopied!
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
3.Copied!
user_input = input['Do you like pizza [yes/no]: '] yes_choices = ['yes', 'y'] no_choices = ['no', 'n'] if user_input.lower[] in yes_choices: print['user typed yes'] elif user_input.lower[] in no_choices: print['user typed no'] else: print['Type yes or no']