Hướng dẫn can we take input in function in python? - chúng ta có thể lấy đầu vào trong chức năng trong python không?

Bạn chưa bao giờ thực sự xác định

Enter roll number 22
Enter age Jessa

Roll number: 22 Name: Jessa
Printing type of a input values
type of number <class 'str'>
type of name <class 'str'>
0 và
Enter roll number 22
Enter age Jessa

Roll number: 22 Name: Jessa
Printing type of a input values
type of number <class 'str'>
type of name <class 'str'>
1 trên toàn cầu. Bạn chỉ xác định nó trong chức năng khi bạn đã làm
Enter roll number 22
Enter age Jessa

Roll number: 22 Name: Jessa
Printing type of a input values
type of number <class 'str'>
type of name <class 'str'>
2.

Show

Khi bạn thực hiện

Enter roll number 22
Enter age Jessa

Roll number: 22 Name: Jessa
Printing type of a input values
type of number <class 'str'>
type of name <class 'str'>
3, bạn không tạo các biến được gọi là
Enter roll number 22
Enter age Jessa

Roll number: 22 Name: Jessa
Printing type of a input values
type of number <class 'str'>
type of name <class 'str'>
0 và
Enter roll number 22
Enter age Jessa

Roll number: 22 Name: Jessa
Printing type of a input values
type of number <class 'str'>
type of name <class 'str'>
1, bạn chỉ đang tạo các tham số cho chức năng của mình.

Để sửa mã của bạn, hãy tạo biến

Enter roll number 22
Enter age Jessa

Roll number: 22 Name: Jessa
Printing type of a input values
type of number <class 'str'>
type of name <class 'str'>
0 và
Enter roll number 22
Enter age Jessa

Roll number: 22 Name: Jessa
Printing type of a input values
type of number <class 'str'>
type of name <class 'str'>
1 trước khi bạn gọi chức năng của mình:

def smaller_num(x, y): ## Can be rephrased to  def smaller_num(x, y):
    if x > y:          ##                          if x > y:
        number = y     ##                              return y
    else:              ##                          else:
        number = x     ##                              return x
return number

x = input("Enter first number:-")
y = input("Enter second number:-")
result = smaller_num(x, y)
print("The smaller number between " +  str(x) + " and " + str(y) + " is " + str(result))

Lý do khác Mã của bạn không hoạt động là vì bạn không gán giá trị trả về của hàm trở lại thành một biến. Khi bạn

Enter roll number 22
Enter age Jessa

Roll number: 22 Name: Jessa
Printing type of a input values
type of number <class 'str'>
type of name <class 'str'>
8 một cái gì đó từ một hàm và một lần nữa khi bạn gọi hàm, bạn cần gán giá trị cho một biến, như tôi có:
Enter roll number 22
Enter age Jessa

Roll number: 22 Name: Jessa
Printing type of a input values
type of number <class 'str'>
type of name <class 'str'>
9.

Khi bạn gọi chức năng của mình, bạn không bao giờ gán giá trị cho một biến, vì vậy nó đã bị lãng phí.


Ngoài ra, bạn đang sử dụng Python 3 hoặc 2.7? Trong Python 3 sử dụng

# program to calculate addition of two input integer numbers

# convert inout into int
first_number = int(input("Enter first number "))
second_number = int(input("Enter second number "))

print("\n")
print("First Number:", first_number)
print("Second Number:", second_number)
sum1 = first_number + second_number
print("Addition of two number is: ", sum1)
0 sẽ trả về một chuỗi và để chuyển đổi nó thành một số nguyên, bạn có thể gọi
# program to calculate addition of two input integer numbers

# convert inout into int
first_number = int(input("Enter first number "))
second_number = int(input("Enter second number "))

print("\n")
print("First Number:", first_number)
print("Second Number:", second_number)
sum1 = first_number + second_number
print("Addition of two number is: ", sum1)
1 xung quanh hàm
# program to calculate addition of two input integer numbers

# convert inout into int
first_number = int(input("Enter first number "))
second_number = int(input("Enter second number "))

print("\n")
print("First Number:", first_number)
print("Second Number:", second_number)
sum1 = first_number + second_number
print("Addition of two number is: ", sum1)
0.

Hãy cùng xem cách sử dụng & nbsp; ________ 125, & nbsp; & nbsp;

Sau khi đọc bài viết này, bạn sẽ học:

  • Đầu vào và đầu ra trong Python
  • Cách nhận đầu vào từ người dùng, tệp và hiển thị đầu ra trên màn hình, bảng điều khiển hoặc ghi nó vào tệp.
  • Lấy số nguyên, float, ký tự và đầu vào chuỗi từ người dùng.
  • Chuyển đổi đầu vào của người dùng thành một loại dữ liệu khác.
  • Đầu vào dòng lệnh
  • Làm thế nào để định dạng đầu ra.

Hàm Python # program to calculate addition of two input integer numbers # convert inout into int first_number = int(input("Enter first number ")) second_number = int(input("Enter second number ")) print("\n") print("First Number:", first_number) print("Second Number:", second_number) sum1 = first_number + second_number print("Addition of two number is: ", sum1)6

Trong Python 3, chúng tôi có hai chức năng tích hợp sau để xử lý đầu vào từ người dùng và hệ thống.

  1. # program to calculate addition of two input integer numbers
    
    # convert inout into int
    first_number = int(input("Enter first number "))
    second_number = int(input("Enter second number "))
    
    print("\n")
    print("First Number:", first_number)
    print("Second Number:", second_number)
    sum1 = first_number + second_number
    print("Addition of two number is: ", sum1)
    7: chấp nhận đầu vào từ người dùng.
  2. # program to calculate addition of two input integer numbers
    
    # convert inout into int
    first_number = int(input("Enter first number "))
    second_number = int(input("Enter second number "))
    
    print("\n")
    print("First Number:", first_number)
    print("Second Number:", second_number)
    sum1 = first_number + second_number
    print("Addition of two number is: ", sum1)
    4: Để hiển thị đầu ra trên bảng điều khiển/màn hình.

Trong Python 2, chúng ta có thể sử dụng hai chức năng sau:

  1. # program to calculate addition of two input integer numbers
    
    # convert inout into int
    first_number = int(input("Enter first number "))
    second_number = int(input("Enter second number "))
    
    print("\n")
    print("First Number:", first_number)
    print("Second Number:", second_number)
    sum1 = first_number + second_number
    print("Addition of two number is: ", sum1)
    9
  2. Enter first number 28
    Enter second number 12
    
    First Number: 28
    Second Number: 12
    Addition of two number is:  40
    0

Hàm

# program to calculate addition of two input integer numbers

# convert inout into int
first_number = int(input("Enter first number "))
second_number = int(input("Enter second number "))

print("\n")
print("First Number:", first_number)
print("Second Number:", second_number)
sum1 = first_number + second_number
print("Addition of two number is: ", sum1)
0 đọc một dòng được nhập trên bảng điều khiển hoặc màn hình bằng thiết bị đầu vào như bàn phím, chuyển đổi nó thành một chuỗi. Là một nhà phát triển mới, điều cần thiết là phải hiểu những gì đầu vào trong Python.

Đầu vào là gì?

Đầu vào là một giá trị được cung cấp bởi hệ thống hoặc người dùng. Ví dụ: giả sử bạn muốn tính toán việc bổ sung hai số trên máy tính, bạn cần cung cấp hai số cho máy tính. Trong trường hợp đó, hai số đó không là gì ngoài một đầu vào do người dùng cung cấp cho chương trình máy tính.. For example, suppose you want to calculate the addition of two numbers on the calculator, you need to provide two numbers to the calculator. In that case, those two number is nothing but an input provided by the user to a calculator program.

Hướng dẫn can we take input in function in python? - chúng ta có thể lấy đầu vào trong chức năng trong python không?

Có nhiều loại thiết bị đầu vào khác nhau mà chúng tôi có thể sử dụng để cung cấp dữ liệu cho ứng dụng. Ví dụ: -

  • Xuất phát từ bàn phím: Người dùng đã nhập một số giá trị bằng bàn phím.: User entered some value using a keyboard.
  • Sử dụng chuột nhấp chuột hoặc chuyển động: Người dùng nhấp vào nút radio hoặc một số danh sách thả xuống và chọn tùy chọn từ nó bằng chuột.: The user clicked on the radio button or some drop-down list and chosen an option from it using mouse.

Trong Python, có nhiều cách khác nhau để đọc đầu vào từ người dùng từ môi trường dòng lệnh hoặc thông qua giao diện người dùng. Trong cả hai trường hợp, người dùng đang gửi thông tin bằng bàn phím hoặc chuột.

Ví dụ về Python để chấp nhận đầu vào từ người dùng

Hãy xem cách chấp nhận thông tin nhân viên từ người dùng.

  • Đầu tiên, hãy hỏi tên nhân viên, tiền lương và tên công ty từ người dùng
  • Tiếp theo, chúng tôi sẽ gán đầu vào do người dùng cung cấp cho các biến
  • Cuối cùng, chúng tôi sẽ sử dụng chức năng
    # program to calculate addition of two input integer numbers
    
    # convert inout into int
    first_number = int(input("Enter first number "))
    second_number = int(input("Enter second number "))
    
    print("\n")
    print("First Number:", first_number)
    print("Second Number:", second_number)
    sum1 = first_number + second_number
    print("Addition of two number is: ", sum1)
    4 để hiển thị các biến đó trên màn hình.
# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)

Output::

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google

Hàm # program to calculate addition of two input integer numbers # convert inout into int first_number = int(input("Enter first number ")) second_number = int(input("Enter second number ")) print("\n") print("First Number:", first_number) print("Second Number:", second_number) sum1 = first_number + second_number print("Addition of two number is: ", sum1)0 hoạt động

Cú pháp

input([prompt])
  • Đối số
    Enter first number 28
    Enter second number 12
    
    First Number: 28
    Second Number: 12
    Addition of two number is:  40
    4 là tùy chọn. Đối số
    Enter first number 28
    Enter second number 12
    
    First Number: 28
    Second Number: 12
    Addition of two number is:  40
    4 được sử dụng để hiển thị thông báo cho người dùng. Ví dụ: lời nhắc là, "Vui lòng nhập tên của bạn."
  • Khi hàm
    # program to calculate addition of two input integer numbers
    
    # convert inout into int
    first_number = int(input("Enter first number "))
    second_number = int(input("Enter second number "))
    
    print("\n")
    print("First Number:", first_number)
    print("Second Number:", second_number)
    sum1 = first_number + second_number
    print("Addition of two number is: ", sum1)
    0 thực thi, chương trình đợi cho đến khi người dùng nhập một số giá trị.
  • Tiếp theo, người dùng nhập một số giá trị trên màn hình bằng bàn phím.
  • Cuối cùng, hàm
    # program to calculate addition of two input integer numbers
    
    # convert inout into int
    first_number = int(input("Enter first number "))
    second_number = int(input("Enter second number "))
    
    print("\n")
    print("First Number:", first_number)
    print("Second Number:", second_number)
    sum1 = first_number + second_number
    print("Addition of two number is: ", sum1)
    0 đọc một giá trị từ màn hình, chuyển đổi nó thành một chuỗi và trả nó về chương trình gọi.

Lưu ý: Nếu bạn nhập một số nguyên hoặc số float, vẫn vậy, nó sẽ chuyển đổi nó thành một chuỗi. Nếu bạn muốn đánh số đầu vào hoặc nhập trong các loại dữ liệu khác, bạn cần thực hiện chuyển đổi loại trên giá trị đầu vào.: If you enter an integer or float number, still, it will convert it into a string. If you want to number input or input in other data types, you need to perform type conversion on the input value.

Hãy để hiểu điều này với một ví dụ.

Ví dụ để kiểm tra kiểu dữ liệu của giá trị đầu vào

number = input("Enter roll number ")
name = input("Enter age ")

print("\n")
print('Roll number:', number, 'Name:', name)
print("Printing type of a input values")
print("type of number", type(number))
print("type of name", type(name))

Output::

Enter roll number 22
Enter age Jessa

Roll number: 22 Name: Jessa
Printing type of a input values
type of number <class 'str'>
type of name <class 'str'>

Như bạn đã biết bất cứ điều gì bạn nhập làm đầu vào, hàm

# program to calculate addition of two input integer numbers

# convert inout into int
first_number = int(input("Enter first number "))
second_number = int(input("Enter second number "))

print("\n")
print("First Number:", first_number)
print("Second Number:", second_number)
sum1 = first_number + second_number
print("Addition of two number is: ", sum1)
0 luôn chuyển đổi nó thành một chuỗi.

Đọc cách kiểm tra xem đầu vào của người dùng có phải là số hoặc chuỗi không.How to check if user input is a number or string.

Lấy số nguyên làm đầu vào từ người dùng

Hãy cùng xem cách chấp nhận giá trị số nguyên từ người dùng trong Python. Chúng ta cần chuyển đổi giá trị chuỗi đầu vào thành một số nguyên bằng hàm

# program to calculate addition of two input integer numbers

# convert inout into int
first_number = int(input("Enter first number "))
second_number = int(input("Enter second number "))

print("\n")
print("First Number:", first_number)
print("Second Number:", second_number)
sum1 = first_number + second_number
print("Addition of two number is: ", sum1)
1.

Example::

# program to calculate addition of two input integer numbers

# convert inout into int
first_number = int(input("Enter first number "))
second_number = int(input("Enter second number "))

print("\n")
print("First Number:", first_number)
print("Second Number:", second_number)
sum1 = first_number + second_number
print("Addition of two number is: ", sum1)

Output::

Enter first number 28
Enter second number 12

First Number: 28
Second Number: 12
Addition of two number is:  40

Lưu ý: Như bạn có thể thấy, chúng tôi đã thêm một cách rõ ràng một loại số nguyên vào hàm đầu vào để chuyển đổi giá trị đầu vào thành loại số nguyên.: As you can see, we explicitly added a cast of an integer type to an input function to convert an input value to the integer type.

Bây giờ nếu bạn in loại & nbsp; & nbsp; first_number bạn sẽ nhận được loại số nguyên. & Nbsp; & nbsp; ________ 80 & nbsp;

Lấy số float làm đầu vào từ người dùng

Giống như số nguyên, chúng ta cần chuyển đổi đầu vào của người dùng thành số float bằng hàm

marks = float(input("Enter marks "))
print("\n")
print("Student marks is: ", marks)
print("type is:", type(marks))
2

marks = float(input("Enter marks "))
print("\n")
print("Student marks is: ", marks)
print("type is:", type(marks))

Output::

Enter marks 74.65

Student marks is:  74.65
type is: <class 'float'>

Thực hành vấn đề

Chấp nhận một số nguyên và một số float từ người dùng và tính toán phép nhân của cả hai số.

Hiển thị giải pháp

# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
0

Nhận nhiều đầu vào từ người dùng trong một dòng

Trong Python, có thể nhận được nhiều giá trị từ người dùng trong một dòng. Chúng tôi có thể chấp nhận hai hoặc ba giá trị từ người dùng.

Ví dụ, trong một lần thực hiện chức năng

# program to calculate addition of two input integer numbers

# convert inout into int
first_number = int(input("Enter first number "))
second_number = int(input("Enter second number "))

print("\n")
print("First Number:", first_number)
print("Second Number:", second_number)
sum1 = first_number + second_number
print("Addition of two number is: ", sum1)
0, chúng ta có thể hỏi người dùng tên, tuổi và số điện thoại của người dùng và lưu trữ nó trong ba biến khác nhau.

Hãy để xem cách làm điều này.

  • Lấy từng đầu vào được phân tách bằng không gian
  • Chia chuỗi đầu vào bằng cách sử dụng
    marks = float(input("Enter marks "))
    print("\n")
    print("Student marks is: ", marks)
    print("type is:", type(marks))
    4 Nhận giá trị của đầu vào riêng lẻ
# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
1

Output::

# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
2

Ngoài ra, bạn có thể lấy danh sách làm đầu vào từ người dùng để nhận và lưu trữ nhiều giá trị cùng một lúc.

Đọc: Cách lấy danh sách làm đầu vào từ người dùng.How to take a list as an input from a user.

Chấp nhận đầu vào đa dòng từ người dùng

Như bạn đã biết, hàm

# program to calculate addition of two input integer numbers

# convert inout into int
first_number = int(input("Enter first number "))
second_number = int(input("Enter second number "))

print("\n")
print("First Number:", first_number)
print("Second Number:", second_number)
sum1 = first_number + second_number
print("Addition of two number is: ", sum1)
0 không cho phép người dùng cung cấp các giá trị được phân tách bằng một dòng mới.

Nếu người dùng cố gắng nhập đầu vào đa dòng, nó chỉ đọc dòng đầu tiên. Bởi vì bất cứ khi nào người dùng nhấn phím Enter, hàm đầu vào sẽ đọc thông tin do người dùng cung cấp và dừng thực thi.

Hãy để xem cách nhận được nhiều dòng đầu vào.

Chúng ta có thể sử dụng một vòng lặp. Trong mỗi lần lặp của vòng lặp, chúng ta có thể nhận các chuỗi đầu vào từ người dùng và tham gia chúng. Bạn cũng có thể kết hợp từng chuỗi đầu vào bằng toán tử

marks = float(input("Enter marks "))
print("\n")
print("Student marks is: ", marks)
print("type is:", type(marks))
6 được phân tách bởi Newline (
marks = float(input("Enter marks "))
print("\n")
print("Student marks is: ", marks)
print("type is:", type(marks))
7).

Example::

# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
3

Output::

# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
4

Python # program to calculate addition of two input integer numbers # convert inout into int first_number = int(input("Enter first number ")) second_number = int(input("Enter second number ")) print("\n") print("First Number:", first_number) print("Second Number:", second_number) sum1 = first_number + second_number print("Addition of two number is: ", sum1)6 vs marks = float(input("Enter marks ")) print("\n") print("Student marks is: ", marks) print("type is:", type(marks))9

  • Hàm
    # program to calculate addition of two input integer numbers
    
    # convert inout into int
    first_number = int(input("Enter first number "))
    second_number = int(input("Enter second number "))
    
    print("\n")
    print("First Number:", first_number)
    print("Second Number:", second_number)
    sum1 = first_number + second_number
    print("Addition of two number is: ", sum1)
    0 hoạt động khác nhau giữa Python 3 và Python 2.
  • Trong Python 2, chúng ta có thể sử dụng cả hàm
    # program to calculate addition of two input integer numbers
    
    # convert inout into int
    first_number = int(input("Enter first number "))
    second_number = int(input("Enter second number "))
    
    print("\n")
    print("First Number:", first_number)
    print("Second Number:", second_number)
    sum1 = first_number + second_number
    print("Addition of two number is: ", sum1)
    0 và
    marks = float(input("Enter marks "))
    print("\n")
    print("Student marks is: ", marks)
    print("type is:", type(marks))
    9 để chấp nhận đầu vào của người dùng.
  • Trong Python 3, hàm
    marks = float(input("Enter marks "))
    print("\n")
    print("Student marks is: ", marks)
    print("type is:", type(marks))
    9 của Python 2 được đổi tên thành
    # program to calculate addition of two input integer numbers
    
    # convert inout into int
    first_number = int(input("Enter first number "))
    second_number = int(input("Enter second number "))
    
    print("\n")
    print("First Number:", first_number)
    print("Second Number:", second_number)
    sum1 = first_number + second_number
    print("Addition of two number is: ", sum1)
    0 và hàm
    # program to calculate addition of two input integer numbers
    
    # convert inout into int
    first_number = int(input("Enter first number "))
    second_number = int(input("Enter second number "))
    
    print("\n")
    print("First Number:", first_number)
    print("Second Number:", second_number)
    sum1 = first_number + second_number
    print("Addition of two number is: ", sum1)
    0 ban đầu bị loại bỏ.

Sự khác biệt giữa các hàm

# program to calculate addition of two input integer numbers

# convert inout into int
first_number = int(input("Enter first number "))
second_number = int(input("Enter second number "))

print("\n")
print("First Number:", first_number)
print("Second Number:", second_number)
sum1 = first_number + second_number
print("Addition of two number is: ", sum1)
0 và
marks = float(input("Enter marks "))
print("\n")
print("Student marks is: ", marks)
print("type is:", type(marks))
9 chỉ có liên quan khi sử dụng Python 2.

  • Sự khác biệt chính giữa hai hàm đó là chức năng
    # program to calculate addition of two input integer numbers
    
    # convert inout into int
    first_number = int(input("Enter first number "))
    second_number = int(input("Enter second number "))
    
    print("\n")
    print("First Number:", first_number)
    print("Second Number:", second_number)
    sum1 = first_number + second_number
    print("Addition of two number is: ", sum1)
    0 tự động chuyển đổi đầu vào người dùng thành loại thích hợp. tức là, nếu một hàm đầu vào () nhập vào người dùng sẽ chuyển đổi nó thành một chuỗi và nếu người dùng nhập một số, nó sẽ chuyển đổi thành số nguyên.
  • marks = float(input("Enter marks "))
    print("\n")
    print("Student marks is: ", marks)
    print("type is:", type(marks))
    9 Chuyển đổi mọi đầu vào của người dùng thành một chuỗi.

Hãy cùng xem cách sử dụng raw_input () trong Python 2.

Ví dụ 1: Chức năng Python 2

marks = float(input("Enter marks "))
print("\n")
print("Student marks is: ", marks)
print("type is:", type(marks))
9 để lấy đầu vào từ người dùng: Python 2
marks = float(input("Enter marks "))
print("\n")
print("Student marks is: ", marks)
print("type is:", type(marks))
9 function to take input from a user

# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
5

Output::

# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
6

Lưu ý: Như bạn có thể thấy, Raw_Input () đã chuyển đổi tất cả các giá trị người dùng thành loại chuỗi.: As you can see, raw_input() converted all user values to string type.

Ví dụ 2: Chức năng Python 2

# program to calculate addition of two input integer numbers

# convert inout into int
first_number = int(input("Enter first number "))
second_number = int(input("Enter second number "))

print("\n")
print("First Number:", first_number)
print("Second Number:", second_number)
sum1 = first_number + second_number
print("Addition of two number is: ", sum1)
0 để lấy đầu vào từ người dùng: Python 2
# program to calculate addition of two input integer numbers

# convert inout into int
first_number = int(input("Enter first number "))
second_number = int(input("Enter second number "))

print("\n")
print("First Number:", first_number)
print("Second Number:", second_number)
sum1 = first_number + second_number
print("Addition of two number is: ", sum1)
0 function to take input from a user

# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
7

Output::

# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
8

Lưu ý: Như bạn có thể thấy, input () đã chuyển đổi tất cả các giá trị người dùng thành kiểu dữ liệu phù hợp.: As you can see, input() converted all user values to appropriate data type.

Lưu ý: Để có được hành vi này của Input () trong Python 3, hãy sử dụng

# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
02: To get the this behavior of input() in Python 3, use
# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
02

Đầu vào dòng lệnh

A & nbsp; Giao diện dòng lệnh (CLI) & nbsp; is & nbsp; một màn hình lệnh & nbsp; hoặc & nbsp; giao diện văn bản & nbsp; được gọi là a & nbsp; shell & nbsp; cho phép & nbsp; người dùng tương tác với một chương trình. & Nbsp;a command screen or text interface called a shell that allows users to interact with a program. 

Ví dụ: trên Windows, chúng tôi sử dụng & nbsp; Prompt Prompt & nbsp; và & nbsp; bash & nbsp; trên linux. Dòng lệnh hoặc giao diện dòng lệnh là một ứng dụng dựa trên văn bản để xem, xử lý và thao tác các tệp trên máy tính của chúng tôi. Dòng lệnh còn được gọi là CMD, CLI, nhắc nhở, bảng điều khiển hoặc thiết bị đầu cuối.

Trên dòng lệnh, chúng tôi thực thi chương trình hoặc lệnh bằng cách cung cấp đầu vào/đối số cho nó. Ngoài ra, đầu ra và lỗi được hiển thị một dòng lệnh.

Chúng ta có thể chạy các chương trình Python trên dòng lệnh. Đầu vào dòng lệnh là một đối số mà chúng tôi chuyển đến chương trình trong thời gian chạy.

Python cung cấp các mô-đun sau để làm việc với các đối số dòng lệnh.

  1. # take three values from user
    name = input("Enter Employee Name: ")
    salary = input("Enter salary: ")
    company = input("Enter Company name: ")
    
    # Display all values on screen
    print("\n")
    print("Printing Employee Details")
    print("Name", "Salary", "Company")
    print(name, salary, company)
    03 Mô -đun
  2. # take three values from user
    name = input("Enter Employee Name: ")
    salary = input("Enter salary: ")
    company = input("Enter Company name: ")
    
    # Display all values on screen
    print("\n")
    print("Printing Employee Details")
    print("Name", "Salary", "Company")
    print(name, salary, company)
    04M Odule
  3. # take three values from user
    name = input("Enter Employee Name: ")
    salary = input("Enter salary: ")
    company = input("Enter Company name: ")
    
    # Display all values on screen
    print("\n")
    print("Printing Employee Details")
    print("Name", "Salary", "Company")
    print(name, salary, company)
    05 Mô -đun
  4. # take three values from user
    name = input("Enter Employee Name: ")
    salary = input("Enter salary: ")
    company = input("Enter Company name: ")
    
    # Display all values on screen
    print("\n")
    print("Printing Employee Details")
    print("Name", "Salary", "Company")
    print(name, salary, company)
    06 Mô -đun
  5. # take three values from user
    name = input("Enter Employee Name: ")
    salary = input("Enter salary: ")
    company = input("Enter Company name: ")
    
    # Display all values on screen
    print("\n")
    print("Printing Employee Details")
    print("Name", "Salary", "Company")
    print(name, salary, company)
    07 Mô -đun

Mô -đun Python Sys

Mô-đun Python

# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
03 là mô-đun cơ bản thực hiện các đối số dòng lệnh trong cấu trúc
# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
09 đơn giản có tên
# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
10.

  • # take three values from user
    name = input("Enter Employee Name: ")
    salary = input("Enter salary: ")
    company = input("Enter Company name: ")
    
    # Display all values on screen
    print("\n")
    print("Printing Employee Details")
    print("Name", "Salary", "Company")
    print(name, salary, company)
    11: Đối số đầu tiên luôn là tên chương trình/tập lệnh.
  • # take three values from user
    name = input("Enter Employee Name: ")
    salary = input("Enter salary: ")
    company = input("Enter Company name: ")
    
    # Display all values on screen
    print("\n")
    print("Printing Employee Details")
    print("Name", "Salary", "Company")
    print(name, salary, company)
    10: Trả về danh sách các đối số dòng lệnh.
  • # take three values from user
    name = input("Enter Employee Name: ")
    salary = input("Enter salary: ")
    company = input("Enter Company name: ")
    
    # Display all values on screen
    print("\n")
    print("Printing Employee Details")
    print("Name", "Salary", "Company")
    print(name, salary, company)
    13: Số lượng đối số dòng lệnh.

Steps:

Viết mã dưới đây trong một tệp và lưu nó dưới dạng

# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
14

# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
9

Chạy lệnh bên dưới trên dòng lệnh

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
0

Đầu ra

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
1

Ở đây 10, 20, 30 là các đối số dòng lệnh được chuyển cho chương trình. Mỗi đầu vào đại diện cho một đối số duy nhất.

  • Đối số đầu tiên, tức là,
    # take three values from user
    name = input("Enter Employee Name: ")
    salary = input("Enter salary: ")
    company = input("Enter Company name: ")
    
    # Display all values on screen
    print("\n")
    print("Printing Employee Details")
    print("Name", "Salary", "Company")
    print(name, salary, company)
    11, luôn đại diện cho tệp chương trình Python (
    # take three values from user
    name = input("Enter Employee Name: ")
    salary = input("Enter salary: ")
    company = input("Enter Company name: ")
    
    # Display all values on screen
    print("\n")
    print("Printing Employee Details")
    print("Name", "Salary", "Company")
    print(name, salary, company)
    16)
  • Các yếu tố danh sách khác, tức là,
    # take three values from user
    name = input("Enter Employee Name: ")
    salary = input("Enter salary: ")
    company = input("Enter Company name: ")
    
    # Display all values on screen
    print("\n")
    print("Printing Employee Details")
    print("Name", "Salary", "Company")
    print(name, salary, company)
    17 đến
    # take three values from user
    name = input("Enter Employee Name: ")
    salary = input("Enter salary: ")
    company = input("Enter Company name: ")
    
    # Display all values on screen
    print("\n")
    print("Printing Employee Details")
    print("Name", "Salary", "Company")
    print(name, salary, company)
    18 là các đối số dòng lệnh. Không gian được sử dụng để tách từng đối số.

Lưu ý:

# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
19 không phải là một mảng. Nó là một danh sách. Đây là một cách đơn giản để đọc các đối số dòng lệnh dưới dạng chuỗi. Xem ví dụ sau để kiểm tra loại
# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
19

Thí dụ

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
2

Bây giờ, hãy để xem một ví dụ khác, nơi chúng tôi hiển thị tất cả các đối số dòng lệnh được truyền cho chương trình.

Ví dụ: Để hiển thị các cuộc tranh luận dòng lệnh

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
3

Chạy lệnh bên dưới trên dòng lệnh

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
0

Đầu ra

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
5

Ở đây 10, 20, 30 là các đối số dòng lệnh được chuyển cho chương trình. Mỗi đầu vào đại diện cho một đối số duy nhất. : The space is separator between command line arguments.

Đối số đầu tiên, tức là,

# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
11, luôn đại diện cho tệp chương trình Python (
# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
16)

Các yếu tố danh sách khác, tức là,

# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
17 đến
# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
18 là các đối số dòng lệnh. Không gian được sử dụng để tách từng đối số.

Thí dụ

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
6

Đầu ra

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
7

Ở đây 10, 20, 30 là các đối số dòng lệnh được chuyển cho chương trình. Mỗi đầu vào đại diện cho một đối số duy nhất.

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
8

Đầu ra

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
9

Ở đây 10, 20, 30 là các đối số dòng lệnh được chuyển cho chương trình. Mỗi đầu vào đại diện cho một đối số duy nhất.

Đối số đầu tiên, tức là,

# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
11, luôn đại diện cho tệp chương trình Python (
# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
16)

Các yếu tố danh sách khác, tức là,

# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
17 đến
# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
18 là các đối số dòng lệnh. Không gian được sử dụng để tách từng đối số.
1: Display output on screen

input([prompt])
0

Lưu ý:

# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
19 không phải là một mảng. Nó là một danh sách. Đây là một cách đơn giản để đọc các đối số dòng lệnh dưới dạng chuỗi. Xem ví dụ sau để kiểm tra loại
# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
19
:

input([prompt])
1

Thí dụ: Display Output by separating each value

input([prompt])
2

Output::

input([prompt])
3

Bây giờ, hãy để xem một ví dụ khác, nơi chúng tôi hiển thị tất cả các đối số dòng lệnh được truyền cho chương trình.

Ví dụ: Để hiển thị các cuộc tranh luận dòng lệnh

Bạn có thể hiển thị đầu ra trong các kiểu và định dạng khác nhau bằng các chức năng sau.

  • # take three values from user
    name = input("Enter Employee Name: ")
    salary = input("Enter salary: ")
    company = input("Enter Company name: ")
    
    # Display all values on screen
    print("\n")
    print("Printing Employee Details")
    print("Name", "Salary", "Company")
    print(name, salary, company)
    23
  • # take three values from user
    name = input("Enter Employee Name: ")
    salary = input("Enter salary: ")
    company = input("Enter Company name: ")
    
    # Display all values on screen
    print("\n")
    print("Printing Employee Details")
    print("Name", "Salary", "Company")
    print(name, salary, company)
    24
  • # take three values from user
    name = input("Enter Employee Name: ")
    salary = input("Enter salary: ")
    company = input("Enter Company name: ")
    
    # Display all values on screen
    print("\n")
    print("Printing Employee Details")
    print("Name", "Salary", "Company")
    print(name, salary, company)
    25,
    # take three values from user
    name = input("Enter Employee Name: ")
    salary = input("Enter salary: ")
    company = input("Enter Company name: ")
    
    # Display all values on screen
    print("\n")
    print("Printing Employee Details")
    print("Name", "Salary", "Company")
    print(name, salary, company)
    26 , and 
    # take three values from user
    name = input("Enter Employee Name: ")
    salary = input("Enter salary: ")
    company = input("Enter Company name: ")
    
    # Display all values on screen
    print("\n")
    print("Printing Employee Details")
    print("Name", "Salary", "Company")
    print(name, salary, company)
    27.
    ,
    # take three values from user
    name = input("Enter Employee Name: ")
    salary = input("Enter salary: ")
    company = input("Enter Company name: ")
    
    # Display all values on screen
    print("\n")
    print("Printing Employee Details")
    print("Name", "Salary", "Company")
    print(name, salary, company)
    26
     , and 
    # take three values from user
    name = input("Enter Employee Name: ")
    salary = input("Enter salary: ")
    company = input("Enter Company name: ")
    
    # Display all values on screen
    print("\n")
    print("Printing Employee Details")
    print("Name", "Salary", "Company")
    print(name, salary, company)
    27
    .
  • # take three values from user
    name = input("Enter Employee Name: ")
    salary = input("Enter salary: ")
    company = input("Enter Company name: ")
    
    # Display all values on screen
    print("\n")
    print("Printing Employee Details")
    print("Name", "Salary", "Company")
    print(name, salary, company)
    28
  • Toán tử & nbsp; ________ 129 cũng có thể sử dụng cho định dạng đầu ra
    # take three values from user
    name = input("Enter Employee Name: ")
    salary = input("Enter salary: ")
    company = input("Enter Company name: ")
    
    # Display all values on screen
    print("\n")
    print("Printing Employee Details")
    print("Name", "Salary", "Company")
    print(name, salary, company)
    29
    operator can also use for output formatting

Bây giờ, hãy xem từng cái một.

# take three values from user name = input("Enter Employee Name: ") salary = input("Enter salary: ") company = input("Enter Company name: ") # Display all values on screen print("\n") print("Printing Employee Details") print("Name", "Salary", "Company") print(name, salary, company)23 để định dạng đầu ra

input([prompt])
4
  • # take three values from user
    name = input("Enter Employee Name: ")
    salary = input("Enter salary: ")
    company = input("Enter Company name: ")
    
    # Display all values on screen
    print("\n")
    print("Printing Employee Details")
    print("Name", "Salary", "Company")
    print(name, salary, company)
    31 là chuỗi mà phương thức định dạng được gọi. Nó có thể chứa các trường văn bản hoặc thay thế được phân định bằng niềng răng {}.
  • Mỗi trường thay thế chứa chỉ số số của một đối số vị trí có trong phương thức định dạng hoặc tên của đối số từ khóa.
  • Phương thức định dạng trả về một chuỗi được định dạng dưới dạng đầu ra. Mỗi trường thay thế được thay thế bằng giá trị chuỗi thực tế của đối số tương ứng có trong phương thức định dạng. tức là, args.

Hãy xem điều này với một ví dụ:

input([prompt])
5

Lưu ý: Ở đây {0} và {1} là chỉ mục số của đối số vị trí có trong phương thức định dạng. tức là, {0} = Ault và {1} = Kelly. Bất cứ điều gì không được đặt trong niềng răng {} đều được coi là một văn bản theo nghĩa đen đơn giản.: Here {0} and {1} is the numeric index of a positional argument present in the format method. i.e., {0} = Ault and {1} = Kelly. Anything that not enclosed in braces {} is considered a plain literal text.

Hướng dẫn can we take input in function in python? - chúng ta có thể lấy đầu vào trong chức năng trong python không?
Tùy chọn định dạng đầu ra Python

Hãy xem các cách khác nhau để hiển thị đầu ra bằng phương thức

# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")

# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
32. Bạn có thể tìm thấy các tùy chọn định dạng khác nhau ở đây.

Định dạng chuỗi đầu ra theo vị trí của nó

input([prompt])
6

Output::

input([prompt])
7

Truy cập các đối số chuỗi đầu ra theo tên

input([prompt])
8

Output::

input([prompt])
9

Căn chỉnh đầu ra bằng cách chỉ định chiều rộng

number = input("Enter roll number ")
name = input("Enter age ")

print("\n")
print('Roll number:', number, 'Name:', name)
print("Printing type of a input values")
print("type of number", type(number))
print("type of name", type(name))
0

Output::

number = input("Enter roll number ")
name = input("Enter age ")

print("\n")
print('Roll number:', number, 'Name:', name)
print("Printing type of a input values")
print("type of number", type(number))
print("type of name", type(name))
1

Chỉ định một dấu hiệu trong khi hiển thị số đầu ra

number = input("Enter roll number ")
name = input("Enter age ")

print("\n")
print('Roll number:', number, 'Name:', name)
print("Printing type of a input values")
print("type of number", type(number))
print("type of name", type(name))
2

Output::

number = input("Enter roll number ")
name = input("Enter age ")

print("\n")
print('Roll number:', number, 'Name:', name)
print("Printing type of a input values")
print("type of number", type(number))
print("type of name", type(name))
3

Hiển thị số đầu ra ở nhiều định dạng khác nhau

number = input("Enter roll number ")
name = input("Enter age ")

print("\n")
print('Roll number:', number, 'Name:', name)
print("Printing type of a input values")
print("type of number", type(number))
print("type of name", type(name))
4

Output::

number = input("Enter roll number ")
name = input("Enter age ")

print("\n")
print('Roll number:', number, 'Name:', name)
print("Printing type of a input values")
print("type of number", type(number))
print("type of name", type(name))
5

Hiển thị số dưới dạng phao

number = input("Enter roll number ")
name = input("Enter age ")

print("\n")
print('Roll number:', number, 'Name:', name)
print("Printing type of a input values")
print("type of number", type(number))
print("type of name", type(name))
6

Output::

number = input("Enter roll number ")
name = input("Enter age ")

print("\n")
print('Roll number:', number, 'Name:', name)
print("Printing type of a input values")
print("type of number", type(number))
print("type of name", type(name))
7

Căn chỉnh chuỗi đầu ra

Hãy cùng xem cách sử dụng & nbsp; ________ 125, & nbsp; & nbsp;

number = input("Enter roll number ")
name = input("Enter age ")

print("\n")
print('Roll number:', number, 'Name:', name)
print("Printing type of a input values")
print("type of number", type(number))
print("type of name", type(name))
8

Output::

number = input("Enter roll number ")
name = input("Enter age ")

print("\n")
print('Roll number:', number, 'Name:', name)
print("Printing type of a input values")
print("type of number", type(number))
print("type of name", type(name))
9

Bước tiếp theo

Để thực hành những gì bạn đã học trong bài viết này, tôi đã tạo ra một bài kiểm tra và tập thể dục.Quiz and Exercise.

  • Bài tập đầu vào và đầu ra của Python
  • Đầu vào Python và bài kiểm tra đầu ra

References::

  • Tài liệu đầu vào và đầu ra
  • Hàm python input ()

Một chức năng có thể lấy đầu vào người dùng không?

Bạn có thể lấy đầu vào của người dùng với hàm input (). Điều này chờ đợi đầu vào bàn phím vô định. Nếu bạn thêm một tham số, nó sẽ in văn bản đó trước khi người dùng nhập.. This waits for keyboard input indefinetly. If you add a parameter, it will print that text before the user input.

Chức năng nào được chấp nhận đầu vào trong Python?

Hàm Python Input () hàm Đầu vào () cho phép đầu vào của người dùng.input() Function The input() function allows user input.