Tôi cũng mới ở những thứ này. Đã thực hiện một chút nghiên cứu từ trang web Python.org và một chút hack để có được điều này để làm việc. Hàm RAW_Input đã trở lại một lần nữa, thay đổi từ đầu vào. Đây là những gì tôi nghĩ ra:raw_input function is back again, changed from input. This is what I came up with:
i,j = raw_input["Enter two values: "].split[]
i = int[i]
j = int[j]
Được cấp, mã không thanh lịch như CIN của một lớp lót bằng Cin của C hoặc C ++. Mã Python trông gần hơn với Java [sử dụng một cơ chế hoàn toàn khác với C, C ++ hoặc Python] sao cho mỗi biến cần được xử lý riêng.scanf or C++'s cin. The Python code looks closer to Java [which employs an entirely different mechanism from C, C++ or Python] such that each variable needs to be dealt with separately.
Trong Python, chức năng RAW_Input nhận các ký tự ra khỏi bảng điều khiển và kết hợp chúng thành một STR dưới dạng đầu ra của nó. Khi chỉ một biến được tìm thấy ở phía bên trái của toán tử gán, hàm phân chia sẽ chia STR này thành một danh sách các giá trị STR.raw_input function gets characters off the console and concatenates them into a single str as its output. When just one variable is found on the left-hand-side of the assignment operator, the split function breaks this str into a list of str values .
Trong trường hợp của chúng tôi, một trong đó chúng tôi mong đợi hai biến, chúng tôi có thể nhận các giá trị vào chúng bằng danh sách phân tách bằng dấu phẩy cho số nhận dạng của họ. Giá trị STR sau đó được gán vào các biến được liệt kê. Nếu chúng ta muốn thực hiện số học với các giá trị này, chúng ta cần chuyển đổi chúng thành loại dữ liệu int [hoặc float] số bằng cách sử dụng hàm int hoặc float tích hợp của Python.str values then get assigned into the variables listed. If we want to do arithmetic with these values, we need to convert them into the numeric int [or float] data type using Python's built-in int or float function.
Tôi biết bài đăng này là một câu trả lời cho một bài đăng rất cũ và có lẽ kiến thức đã được đưa ra là "kiến thức phổ biến" trong một thời gian. Tuy nhiên, tôi sẽ đánh giá cao một bài đăng như bài này thay vì tôi phải dành vài giờ để tìm kiếm và hack cho đến khi tôi nghĩ ra những gì tôi cảm thấy là giải pháp thanh lịch nhất có thể được trình bày trong lớp học CS1.
Sử dụng hàm input [] để nhận số từ người dùng ..
Xem thảo luận
Cải thiện bài viết
Sử dụng hàm input [] để nhận số từ người dùng ..
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
Chẳng hạn, trong c, chúng ta có thể làm một cái gì đó như thế này:
Một giải pháp là sử dụng raw_input [] hai lần.
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]
Một giải pháp khác là sử dụng Split []
Lưu ý rằng chúng tôi không phải chỉ định rõ ràng sự phân chia [‘] vì split [] sử dụng bất kỳ ký tự khoảng trắng nào làm dấu phân cách mặc định.
Một điều cần lưu ý trong mã Python trên là, cả X và Y sẽ là chuỗi. Chúng ta có thể chuyển đổi chúng sang int bằng cách sử dụng một dòng khác
Dưới đây là một mã dòng hoàn chỉnh để đọc hai biến số nguyên từ đầu vào tiêu chuẩn bằng cách sử dụng phân chia và danh sách hiểu biếtAbhishek Shukla. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
- Bàn luận
- Nhà phát triển thường muốn người dùng nhập nhiều giá trị hoặc đầu vào trong một dòng. Trong C ++/C, người dùng có thể lấy nhiều đầu vào trong một dòng bằng cách sử dụng Scanf nhưng trong Python, người dùng có thể lấy nhiều giá trị hoặc đầu vào trong một dòng bằng hai phương thức. & NBSP;
Sử dụng phương thức Split []split[] method :
This function helps in getting multiple inputs from users. It breaks the given input by the specified separator. If a separator is not provided then any white space is a
separator. Generally, users use a split[] method to split a Python string but one can use it in taking multiple inputs.
Sử dụng danh sách hiểu
input[].split[separator, maxsplit]
Sử dụng phương thức Split []: & nbsp; Hàm này giúp nhận được nhiều đầu vào từ người dùng. Nó phá vỡ đầu vào đã cho bởi bộ phân cách được chỉ định. Nếu một dải phân cách không được cung cấp thì bất kỳ không gian trắng nào là một dấu tách. Nói chung, người dùng sử dụng phương thức chia [] để phân chia chuỗi Python nhưng người ta có thể sử dụng nó để thực hiện nhiều đầu vào.
Python3
Cú pháp: & nbsp;
input[].split[separator, maxsplit]7
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]6
input[].split[separator, maxsplit]9
x, y
0input[].split[separator, maxsplit]7
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]6
x, y
3x, y
4input[].split[separator, maxsplit]7
x, y
6Ví dụ: & nbsp;
x, y
=
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]1
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]6
input[].split[separator, maxsplit]5
input[].split[separator, maxsplit]6
input[].split[separator, maxsplit]7
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]6
=
9x, y
4input[].split[separator, maxsplit]7
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]6
[
3[
4input[].split[separator, maxsplit]7
x, y
6x, y
7=
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]1
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]6
=
1input[].split[separator, maxsplit]6
input[].split[separator, maxsplit]7
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]6
int
5int
6int
7int
8input[].split[separator, maxsplit]7
x, y
6input[].split[separator, maxsplit]7
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]6
=
5x, y
0input[].split[separator, maxsplit]7
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]6
for
5x, y
0Output:
[
7=
x, y = [int[x], int[y]]
# We can also use list comprehension
x, y = [int[x] for x in [x, y]]
1x, y = [int[x], int[y]]
# We can also use list comprehension
x, y = [int[x] for x in [x, y]]
6input[].split[separator, maxsplit]
5input[].split[separator, maxsplit]
6List comprehension :
List comprehension is an elegant way to define and create list in Python. We can create lists just like mathematical statements in one line only. It is also used in getting multiple inputs from a user.
Example:
Python3
x, y
=
input[].split[separator, maxsplit]7
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]6
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]01
x, y
0input[].split[separator, maxsplit]7
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]6
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]05
x, y
4input[].split[separator, maxsplit]7
x, y
6x, y
7=
input[].split[separator, maxsplit]7
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]6
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]01
x, y
0input[].split[separator, maxsplit]7
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]6
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]05
x, y
4input[].split[separator, maxsplit]7
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]6
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]31
[
4input[].split[separator, maxsplit]7
x, y
6x, y
=
input[].split[separator, maxsplit]7
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]6
int
5int
6int
7x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]52
input[].split[separator, maxsplit]7
x, y
6x
=
input[].split[separator, maxsplit]7
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]6
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]69
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]70
Đầu ra: & nbsp; & nbsp;
Lưu ý: Các ví dụ trên lấy đầu vào được phân tách bằng không gian. Trong trường hợp chúng tôi muốn lấy đầu vào được phân tách bằng dấu phẩy [,], chúng tôi có thể sử dụng các mục sau: & nbsp;The above examples take input separated by spaces. In case we wish to take input separated by comma [, ], we can use the following:
Python3
x
=
input[].split[separator, maxsplit]7
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]6
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]69
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]70
Vui lòng xem //ide.geeksforgeek.org/bhf0cxr4mx để chạy mẫu. & Nbsp;