Hướng dẫn how do you get two integer inputs in the same line in python? - làm thế nào để bạn nhận được hai đầu vào số nguyên trên cùng 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

Khởi tạo một danh sách chứa đầy một số số lặp đi lặp lại:

>>> [1]* 10
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

Xem thảo luận

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

Lưu bài viết

  • Đọc
  • Bàn luận
  • 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 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
    
    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
    
    2(
    Input the value of x & y
     2 4
    The value of x & y are:  2 4
    
    8
    Input the value of x & y
     2 4
    The value of x & y are:  2 4
    
    9

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

    Ví dụ: & nbsp;

    x, y = input(____1010

    Input the value of x & y
     2 4
    The value of x & y are:  2 4
    
    2(
    Input the value of a & b:  2 4
    The value of a & b are: 2 4
    
    4
    Input the value of x & y
     2 4
    The value of x & y are:  2 4
    
    9

    Input the value of x & y
     2 4
    The value of x & y are:  2 4
    
    2(
    Input the value of a & b:  2 4
    The value of a & b are: 2 4
    
    8
    Input the value of a & b:  2 4
    The value of a & b are: 2 4
    
    9

    Input the value of x & y
     2 4
    The value of x & y are:  2 4
    
    2
    a, b = [int(a) for a in input("Input the value of a & b: ").split()]
    print("The value of a & b are:",a,b)
    
    
    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)
    
    
    2= input(
    a, b = [int(a) for a in input("Input the value of a & b: ").split()]
    print("The value of a & b are:",a,b)
    
    
    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().split(separator, maxsplit)
    0
    input().split(separator, maxsplit)
    1
    input().split(separator, maxsplit)
    2
    input().split(separator, maxsplit)
    3

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

    Input the value of x & y
     2 4
    The value of x & y are:  2 4
    
    2(
    Input the value of a & b:  2 4
    The value of a & b are: 2 4
    
    0
    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
    
    2(=0
    Input the value of x & y
     2 4
    The value of x & y are:  2 4
    
    5

    Output:   
     

    Hướng dẫn how do you get two integer inputs in the same line in python? - làm thế nào để bạn nhận được hai đầu vào số nguyên trên cùng một dòng trong python?

    >>> [1]* 10
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
    
    2= input(____1010List 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 do you get two integer inputs in the same line in python? - làm thế nào để bạn nhận được hai đầu vào số nguyên trên cùng một dòng trong python?

    Example:  

    Python3

    x, y =

    Input the value of x & y
     2 4
    The value of x & y are:  2 4
    
    2(input6
    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
    
    2((0
    Input the value of x & y
     2 4
    The value of x & y are:  2 4
    
    9

    Input the value of x & y
     2 4
    The value of x & y are:  2 4
    
    2
    a, b = [int(a) for a in input("Input the value of a & b: ").split()]
    print("The value of a & b are:",a,b)
    
    
    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)
    
    
    2= =4x, y 2=6__

    Input the value of x & y
     2 4
    The value of x & y are:  2 4
    
    2(input6
    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
    
    2((0
    Input the value of x & y
     2 4
    The value of x & y are:  2 4
    
    9

    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
    
    16
    Input the value of a & b:  2 4
    The value of a & b are: 2 4
    
    9

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

    x, y =

    Input the value of x & y
     2 4
    The value of x & y are:  2 4
    
    2(
    input().split(separator, maxsplit)
    0
    input().split(separator, maxsplit)
    1
    input().split(separator, maxsplit)
    2
    Input the value of x & y
     2 4
    The value of x & y are:  2 4
    
    37

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

    input().split(separator, maxsplit)
    6= =4x, y 2=6__

    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
    
    54
    Input the value of x & y
     2 4
    The value of x & y are:  2 4
    
    55

    Đầu ra: & nbsp; & nbsp;
     

    Hướng dẫn how do you get two integer inputs in the same line in python? - làm thế nào để bạn nhận được hai đầu vào số nguyên trên cùng một dòng trong python?

    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

    input().split(separator, maxsplit)
    6= =4x, y 2=6__

    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
    
    54
    Input the value of x & y
     2 4
    The value of x & y are:  2 4
    
    55

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


    Làm thế nào để bạn có được hai đầu vào trong một dòng Python?

    Tuy nhiên, Python cung cấp hai phương pháp giúp chúng tôi lấy nhiều giá trị hoặc đầu vào trong một dòng ...
    # Lấy nhiều đầu vào trong một dòng ..
    # và loại đúc bằng hàm danh sách () ..
    x = list (map (int, input ("Nhập nhiều giá trị:"). split ())).
    In ("Danh sách học sinh:", x).

    Làm thế nào để bạn lấy hai đầu vào trên cùng một dòng?

    a) Chức năng chia () Chức năng giúp chúng tôi nhận được nhiều đầu vào từ người dùng và gán chúng cho các biến tương ứng trong một dòng.Hàm này thường được sử dụng để tách một chuỗi nhất định thành một số chuỗi con.Tuy nhiên, bạn cũng có thể sử dụng nó để lấy nhiều đầu vào.split () split( ) function helps us get multiple inputs from the user and assign them to the respective variables in one line. This function is generally used to separate a given string into several substrings. However, you can also use it for taking multiple inputs.