Tôi hiểu câu hỏi nhiều hơn về việc lặp lại cho đầu vào có liên quan [như @svbz3r0 đã chỉ ra trong bình luận]. Đối với một người rất mới bắt đầu trong Python, có thể khó có thể sao chép mã từ các hướng dẫn, vì vậy:
#! /usr/bin/env python
from __future__ import print_function
# HACK A DID ACK to run unchanged under Python v2 and v3+:
from platform import python_version_tuple as p_v_t
__py_version_3_plus = False if int[p_v_t[][0]] < 3 else True
v23_input = input if __py_version_3_plus else raw_input
valid_ins = ['yes', 'no']
prompt = "One of [{0}]> ".format[', '.join[valid_ins]]
got = None
try:
while True:
got = v23_input[prompt].strip[]
if got in valid_ins:
break
else:
got = None
except KeyboardInterrupt:
print[]
if got is not None:
print["Received {0}".format[got]]
Nên làm việc trên Python V2 và V3 không thay đổi [không có dấu hiệu nào trong câu hỏi và đầu vào [] trong Python V2 có thể không phải là một ý tưởng tốt ...
Chạy mã trên trong Python 3.5.1:
$ python3 so_x_input_loop.py
One of [yes, no]> why
One of [yes, no]> yes
Received yes
Đột phá thông qua Control-C:
$ python3 so_x_input_loop.py
One of [yes, no]> ^C
Khi rõ ràng, trên phiên bản nào để chạy, nói Python V3, hơn mã có thể đơn giản như:
#! /usr/bin/env python
valid_ins = ['yes', 'no']
prompt = "One of [{0}]> ".format[', '.join[valid_ins]]
got = None
try:
while True:
got = input[prompt].strip[]
if got in valid_ins:
break
else:
got = None
except KeyboardInterrupt:
print[]
if got is not None:
print["Received {0}".format[got]]
Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
- đầu vào [nhắc]
- RAW_Input [Nhắc]
Input []: Hàm này trước tiên lấy đầu vào từ người dùng và chuyển đổi nó thành một chuỗi. Loại đối tượng được trả về luôn luôn như vậy. Nó không đánh giá biểu thức mà nó chỉ trả về câu lệnh hoàn chỉnh dưới dạng chuỗi. Ví dụ, Python cung cấp một chức năng tích hợp có tên đầu vào lấy đầu vào từ người dùng. Khi hàm đầu vào được gọi là dừng chương trình và chờ đợi đầu vào của người dùng. Khi người dùng nhấn vào, chương trình sẽ tiếp tục và trả về những gì người dùng đã nhập. & NBSP; This function first takes the input from the user and converts it into a string. The type of the returned object always will be . It does not evaluate the expression it just returns the complete statement as String. For example, Python provides a built-in function called input which takes the input from the user. When the input function is called it stops the program and waits for the user’s input. When the user presses enter, the program resumes and returns what the user typed.
Syntax:
inp = input['STATEMENT'] Example: 1. >>> name = input['What is your name?\n'] # \n ---> newline ---> It causes a line break >>> What is your name? Ram >>> print[name] Ram # ---> comment in python
Python3
val
=
input
[
____1010
$ python3 so_x_input_loop.py
One of [yes, no]> why
One of [yes, no]> yes
Received yes
2$ python3 so_x_input_loop.py
One of [yes, no]> why
One of [yes, no]> yes
Received yes
3Output:
Lấy chuỗi làm đầu vào:
Python3
‘
$ python3 so_x_input_loop.py
One of [yes, no]> why
One of [yes, no]> yes
Received yes
2$ python3 so_x_input_loop.py
One of [yes, no]> ^C
1Output:
What is your name? Ram Ram
Cách hàm đầu vào hoạt động trong Python: & nbsp; & nbsp;
- Khi hàm input [] thực thi luồng chương trình sẽ được dừng cho đến khi người dùng đưa ra đầu vào.
- Văn bản hoặc tin nhắn được hiển thị trên màn hình đầu ra để yêu cầu người dùng nhập giá trị đầu vào là tùy chọn, tức là lời nhắc, sẽ được in trên màn hình là tùy chọn.
- Bất cứ điều gì bạn nhập làm đầu vào, hàm đầu vào chuyển đổi nó thành một chuỗi. Nếu bạn nhập hàm INPUT [] giá trị nguyên sẽ chuyển đổi nó thành một chuỗi. Bạn cần chuyển đổi rõ ràng nó thành một số nguyên trong mã của bạn bằng cách sử dụng typecasting. & NBSP;
Code:
Python3
$ python3 so_x_input_loop.py
One of [yes, no]> ^C
2=
input
[
$ python3 so_x_input_loop.py
One of [yes, no]> ^C
6$ python3 so_x_input_loop.py
One of [yes, no]> why
One of [yes, no]> yes
Received yes
1$ python3 so_x_input_loop.py
One of [yes, no]> why
One of [yes, no]> yes
Received yes
2$ python3 so_x_input_loop.py
One of [yes, no]> ^C
9#! /usr/bin/env python
valid_ins = ['yes', 'no']
prompt = "One of [{0}]> ".format[', '.join[valid_ins]]
got = None
try:
while True:
got = input[prompt].strip[]
if got in valid_ins:
break
else:
got = None
except KeyboardInterrupt:
print[]
if got is not None:
print["Received {0}".format[got]]
0____7 input
[
#! /usr/bin/env python
valid_ins = ['yes', 'no']
prompt = "One of [{0}]> ".format[', '.join[valid_ins]]
got = None
try:
while True:
got = input[prompt].strip[]
if got in valid_ins:
break
else:
got = None
except KeyboardInterrupt:
print[]
if got is not None:
print["Received {0}".format[got]]
4$ python3 so_x_input_loop.py
One of [yes, no]> why
One of [yes, no]> yes
Received yes
1$ python3 so_x_input_loop.py
One of [yes, no]> why
One of [yes, no]> yes
Received yes
2#! /usr/bin/env python
valid_ins = ['yes', 'no']
prompt = "One of [{0}]> ".format[', '.join[valid_ins]]
got = None
try:
while True:
got = input[prompt].strip[]
if got in valid_ins:
break
else:
got = None
except KeyboardInterrupt:
print[]
if got is not None:
print["Received {0}".format[got]]
7$ python3 so_x_input_loop.py
One of [yes, no]> why
One of [yes, no]> yes
Received yes
2 [
inp = input['STATEMENT'] Example: 1. >>> name = input['What is your name?\n'] # \n ---> newline ---> It causes a line break >>> What is your name? Ram >>> print[name] Ram # ---> comment in python0
inp = input['STATEMENT'] Example: 1. >>> name = input['What is your name?\n'] # \n ---> newline ---> It causes a line break >>> What is your name? Ram >>> print[name] Ram # ---> comment in python1
inp = input['STATEMENT'] Example: 1. >>> name = input['What is your name?\n'] # \n ---> newline ---> It causes a line break >>> What is your name? Ram >>> print[name] Ram # ---> comment in python2
inp = input['STATEMENT'] Example: 1. >>> name = input['What is your name?\n'] # \n ---> newline ---> It causes a line break >>> What is your name? Ram >>> print[name] Ram # ---> comment in python3
$ python3 so_x_input_loop.py
One of [yes, no]> why
One of [yes, no]> yes
Received yes
2 [
inp = input['STATEMENT'] Example: 1. >>> name = input['What is your name?\n'] # \n ---> newline ---> It causes a line break >>> What is your name? Ram >>> print[name] Ram # ---> comment in python6
inp = input['STATEMENT'] Example: 1. >>> name = input['What is your name?\n'] # \n ---> newline ---> It causes a line break >>> What is your name? Ram >>> print[name] Ram # ---> comment in python1
inp = input['STATEMENT'] Example: 1. >>> name = input['What is your name?\n'] # \n ---> newline ---> It causes a line break >>> What is your name? Ram >>> print[name] Ram # ---> comment in python2
inp = input['STATEMENT'] Example: 1. >>> name = input['What is your name?\n'] # \n ---> newline ---> It causes a line break >>> What is your name? Ram >>> print[name] Ram # ---> comment in python9
Output:
Raw_Input []: Hàm này hoạt động trong phiên bản cũ hơn [như Python 2.x]. Hàm này lấy chính xác những gì được gõ từ bàn phím, chuyển đổi nó thành chuỗi và sau đó trả nó về biến mà chúng tôi muốn lưu trữ nó. This function works in older version [like Python 2.x]. This function takes exactly what is typed from the keyboard, converts it to string, and then returns it to the variable in which we want to store it.
Example:
Python
What is your name? Ram Ram0____7
What is your name? Ram Ram2
[
What is your name? Ram Ram4
$ python3 so_x_input_loop.py
One of [yes, no]> why
One of [yes, no]> yes
Received yes
1$ python3 so_x_input_loop.py
One of [yes, no]> why
One of [yes, no]> yes
Received yes
2 What is your name? Ram Ram7
Output:
& nbsp; Ở đây, g là một biến sẽ nhận được giá trị chuỗi, được người dùng gõ trong quá trình thực hiện chương trình. Việc gõ dữ liệu cho hàm Raw_Input [] bị chấm dứt bởi khóa nhập. Chúng ta cũng có thể sử dụng raw_input [] để nhập dữ liệu số. Trong trường hợp đó, chúng tôi sử dụng typecasting. Để biết thêm chi tiết về typecasting tham khảo điều này. & NBSP;
Here, g is a variable that will get
the string value, typed by the user during the execution of the program. Typing of data for the raw_input[] function is terminated by enter key. We can use raw_input[] to enter numeric data also. In that case, we use typecasting. For more details on typecasting refer this.
Tham khảo danh sách bài viết làm đầu vào từ người dùng để biết thêm thông tin. & NBSP;