Hướng dẫn how to take two int inputs in one line in python - cách lấy hai đầu vào int trong một dòng trong python

Cập nhật lần cuối vào ngày 19 tháng 8 năm 2022 21:50:48 (UTC/GMT +8 giờ)

Python Basic: Bài tập-134 với giải pháp

Viết một chương trình Python để nhập hai số nguyên vào một dòng.

Giải pháp mẫu-1:

Mã Python:

print("Input the value of x & y")
x, y = map(int, input().split())
print("The value of x & y are: ",x,y)

Đầu ra mẫu:

Input the value of x & y
 2 4
The value of x & y are:  2 4

Trực quan hóa thực thi mã Python:

Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực hiện chương trình đã nói:

Giải pháp mẫu-2:

Mã Python:

a, b = [int(a) for a in input("Input the value of a & b: ").split()]
print("The value of a & b are:",a,b)

Đầu ra mẫu:

Input the value of a & b:  2 4
The value of a & b are: 2 4

Trực quan hóa thực thi mã Python:

Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực hiện chương trình đã nói:

Giải pháp mẫu-2:

Trình chỉnh sửa mã Python:

Có một cách khác để giải quyết giải pháp này? Đóng góp mã của bạn (và nhận xét) thông qua Disqus. Write a Python program to calculate the time runs (difference between start and current time)of a program.
Next: Write a Python program to print a variable without spaces between values.

Python: Lời khuyên trong ngày

Sử dụng toàn bộ danh sách để rút ngắn cho các vòng lặp:

x = [1, 2, 3, 4, 5]
result = []
for idx in range(len(x)):
    result.append(x[idx] * 2)
result

Output:

[2, 4, 6, 8, 10]
[(element * 2) for element in x]

Output:

[2, 4, 6, 8, 10]

Vui lòng xem https://ide.geeksforgeek.org/bhf0cxr4mx để chạy mẫu. & Nbsp;

Cải thiện bài viết

Lưu bài viết

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.

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.

x, y = [int(x), int(y)]

# We can also use  list comprehension
x, y = [int(x) for x in [x, y]]

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ết

Is

____10

Input the value of x & y
 2 4
The value of x & y are:  2 4
1
a, b = [int(a) for a in input("Input the value of a & b: ").split()]
print("The value of a & b are:",a,b)

22.Abhishek Shukla. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


Cải thiện bài viết

Lưu bài viết

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 ()
  • Sử dụng danh sách hiểu

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.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.

Cú pháp: & nbsp; 

input().split(separator, maxsplit)

Ví dụ: & nbsp; 

Python3

____10

Input the value of x & y
 2 4
The value of x & y are:  2 4
1
Input the value of x & y
 2 4
The value of x & y are:  2 4
8
a, b = [int(a) for a in input("Input the value of a & b: ").split()]
print("The value of a & b are:",a,b)

3
Input the value of a & b:  2 4
The value of a & b are: 2 4
2
Input the value of a & b:  2 4
The value of a & b are: 2 4
3

Input the value of a & b:  2 4
The value of a & b are: 2 4
4
a, b = [int(a) for a in input("Input the value of a & b: ").split()]
print("The value of a & b are:",a,b)

3
Input the value of a & b:  2 4
The value of a & b are: 2 4
6
Input the value of a & b:  2 4
The value of a & b are: 2 4
7

Input the value of a & b:  2 4
The value of a & b are: 2 4
4
a, b = [int(a) for a in input("Input the value of a & b: ").split()]
print("The value of a & b are:",a,b)

3
x = [1, 2, 3, 4, 5]
result = []
for idx in range(len(x)):
    result.append(x[idx] * 2)
result
0
x = [1, 2, 3, 4, 5]
result = []
for idx in range(len(x)):
    result.append(x[idx] * 2)
result
1

Input the value of a & b:  2 4
The value of a & b are: 2 4
4
x = [1, 2, 3, 4, 5]
result = []
for idx in range(len(x)):
    result.append(x[idx] * 2)
result
3

x = [1, 2, 3, 4, 5]
result = []
for idx in range(len(x)):
    result.append(x[idx] * 2)
result
4
Input the value of x & y
 2 4
The value of x & y are:  2 4
1
Input the value of x & y
 2 4
The value of x & y are:  2 4
8
a, b = [int(a) for a in input("Input the value of a & b: ").split()]
print("The value of a & b are:",a,b)

3
x = [1, 2, 3, 4, 5]
result = []
for idx in range(len(x)):
    result.append(x[idx] * 2)
result
8
Input the value of a & b:  2 4
The value of a & b are: 2 4
3

Input the value of a & b:  2 4
The value of a & b are: 2 4
4
a, b = [int(a) for a in input("Input the value of a & b: ").split()]
print("The value of a & b are:",a,b)

3
[2, 4, 6, 8, 10]
2
Input the value of a & b:  2 4
The value of a & b are: 2 4
7

Input the value of a & b:  2 4
The value of a & b are: 2 4
4
a, b = [int(a) for a in input("Input the value of a & b: ").split()]
print("The value of a & b are:",a,b)

3
[2, 4, 6, 8, 10]
6
x = [1, 2, 3, 4, 5]
result = []
for idx in range(len(x)):
    result.append(x[idx] * 2)
result
1

Input the value of a & b:  2 4
The value of a & b are: 2 4
4
a, b = [int(a) for a in input("Input the value of a & b: ").split()]
print("The value of a & b are:",a,b)

3
[(element * 2) for element in x]
0
[(element * 2) for element in x]
1

Input the value of a & b:  2 4
The value of a & b are: 2 4
4
x = [1, 2, 3, 4, 5]
result = []
for idx in range(len(x)):
    result.append(x[idx] * 2)
result
3

[(element * 2) for element in x]
4
Input the value of x & y
 2 4
The value of x & y are:  2 4
1
Input the value of x & y
 2 4
The value of x & y are:  2 4
8
a, b = [int(a) for a in input("Input the value of a & b: ").split()]
print("The value of a & b are:",a,b)

3
Input the value of a & b:  2 4
The value of a & b are: 2 4
2
Input the value of a & b:  2 4
The value of a & b are: 2 4
3

Các

Input the value of a & b:  2 4
The value of a & b are: 2 4
4
x = [1, 2, 3, 4, 5]
result = []
for idx in range(len(x)):
    result.append(x[idx] * 2)
result
3

Các

Input the value of a & b:  2 4
The value of a & b are: 2 4
4
a, b = [int(a) for a in input("Input the value of a & b: ").split()]
print("The value of a & b are:",a,b)

3
input().split(separator, maxsplit)
2
Input the value of a & b:  2 4
The value of a & b are: 2 4
7

Output:   
 

Hướng dẫn how to take two int inputs in one line in python - cách lấy hai đầu vào int trong một dòng trong python

Sử dụng danh sách hiểu biết: & NBSP; Danh sách hiểu là một cách thanh lịch để xác định và tạo danh sách trong Python. Chúng ta có thể tạo danh sách giống như các câu lệnh toán học chỉ trong một dòng. Nó cũng được sử dụng để nhận nhiều đầu vào từ người dùng. & NBSP;List 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. 

Hướng dẫn how to take two int inputs in one line in python - cách lấy hai đầu vào int trong một dòng trong python

Example:  

Python3

Is

Input the value of a & b:  2 4
The value of a & b are: 2 4
4
a, b = [int(a) for a in input("Input the value of a & b: ").split()]
print("The value of a & b are:",a,b)

3
Input the value of x & y
 2 4
The value of x & y are:  2 4
08
Input the value of a & b:  2 4
The value of a & b are: 2 4
7

Input the value of a & b:  2 4
The value of a & b are: 2 4
4
a, b = [int(a) for a in input("Input the value of a & b: ").split()]
print("The value of a & b are:",a,b)

3
Input the value of x & y
 2 4
The value of x & y are:  2 4
12
x = [1, 2, 3, 4, 5]
result = []
for idx in range(len(x)):
    result.append(x[idx] * 2)
result
1

Input the value of a & b:  2 4
The value of a & b are: 2 4
4
x = [1, 2, 3, 4, 5]
result = []
for idx in range(len(x)):
    result.append(x[idx] * 2)
result
3

x = [1, 2, 3, 4, 5]
result = []
for idx in range(len(x)):
    result.append(x[idx] * 2)
result
4
Input the value of x & y
 2 4
The value of x & y are:  2 4
1
Input the value of x & y
 2 4
The value of x & y are:  2 4
2
Input the value of x & y
 2 4
The value of x & y are:  2 4
3
Input the value of x & y
 2 4
The value of x & y are:  2 4
4
Input the value of x & y
 2 4
The value of x & y are:  2 4
5

Input the value of a & b:  2 4
The value of a & b are: 2 4
4
a, b = [int(a) for a in input("Input the value of a & b: ").split()]
print("The value of a & b are:",a,b)

3
Input the value of x & y
 2 4
The value of x & y are:  2 4
08
Input the value of a & b:  2 4
The value of a & b are: 2 4
7

Input the value of a & b:  2 4
The value of a & b are: 2 4
4
a, b = [int(a) for a in input("Input the value of a & b: ").split()]
print("The value of a & b are:",a,b)

3
Input the value of x & y
 2 4
The value of x & y are:  2 4
12
x = [1, 2, 3, 4, 5]
result = []
for idx in range(len(x)):
    result.append(x[idx] * 2)
result
1

Input the value of a & b:  2 4
The value of a & b are: 2 4
4
a, b = [int(a) for a in input("Input the value of a & b: ").split()]
print("The value of a & b are:",a,b)

3
Input the value of x & y
 2 4
The value of x & y are:  2 4
38
[(element * 2) for element in x]
1

Input the value of a & b:  2 4
The value of a & b are: 2 4
4
x = [1, 2, 3, 4, 5]
result = []
for idx in range(len(x)):
    result.append(x[idx] * 2)
result
3

Is

Input the value of a & b:  2 4
The value of a & b are: 2 4
4
a, b = [int(a) for a in input("Input the value of a & b: ").split()]
print("The value of a & b are:",a,b)

3
[2, 4, 6, 8, 10]
2
[2, 4, 6, 8, 10]
3
[2, 4, 6, 8, 10]
4
Input the value of x & y
 2 4
The value of x & y are:  2 4
59

Input the value of a & b:  2 4
The value of a & b are: 2 4
4
x = [1, 2, 3, 4, 5]
result = []
for idx in range(len(x)):
    result.append(x[idx] * 2)
result
3

x = [1, 2, 3, 4, 5]
result = []
for idx in range(len(x)):
    result.append(x[idx] * 2)
result
4
Input the value of x & y
 2 4
The value of x & y are:  2 4
1
Input the value of x & y
 2 4
The value of x & y are:  2 4
2
Input the value of x & y
 2 4
The value of x & y are:  2 4
3
Input the value of x & y
 2 4
The value of x & y are:  2 4
4
Input the value of x & y
 2 4
The value of x & y are:  2 4
5

Input the value of a & b:  2 4
The value of a & b are: 2 4
4
a, b = [int(a) for a in input("Input the value of a & b: ").split()]
print("The value of a & b are:",a,b)

3
Input the value of x & y
 2 4
The value of x & y are:  2 4
76
Input the value of x & y
 2 4
The value of x & y are:  2 4
77

Input the value of x & y
 2 4
The value of x & y are:  2 4
6
Input the value of x & y
 2 4
The value of x & y are:  2 4
1
Input the value of x & y
 2 4
The value of x & y are:  2 4
2
Input the value of x & y
 2 4
The value of x & y are:  2 4
3
Input the value of x & y
 2 4
The value of x & y are:  2 4
4
Input the value of x & y
 2 4
The value of x & y are:  2 4
5
Input the value of x & y
 2 4
The value of x & y are:  2 4
6__

 

Hướng dẫn how to take two int inputs in one line in python - cách lấy hai đầu vào int trong một dòng trong python

Đầu ra: & nbsp; & 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

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;

Input the value of a & b:  2 4
The value of a & b are: 2 4
4
a, b = [int(a) for a in input("Input the value of a & b: ").split()]
print("The value of a & b are:",a,b)

3
Input the value of x & y
 2 4
The value of x & y are:  2 4
76
Input the value of x & y
 2 4
The value of x & y are:  2 4
77

Input the value of x & y
 2 4
The value of x & y are:  2 4
6
Input the value of x & y
 2 4
The value of x & y are:  2 4
1
Input the value of x & y
 2 4
The value of x & y are:  2 4
2
Input the value of x & y
 2 4
The value of x & y are:  2 4
3
Input the value of x & y
 2 4
The value of x & y are:  2 4
4
Input the value of x & y
 2 4
The value of x & y are:  2 4
5
Input the value of x & y
 2 4
The value of x & y are:  2 4
6__