Hướng dẫn how do you define names in python 3? - làm thế nào để bạn xác định tên trong python 3?

Bất cứ khi nào tôi chạy mã của mình, tôi nhận được: nameerror: name 'object_oriented_programming' không được xác định

Làm cách nào để xác định tên Object_oriented_programming?

Code:

class Object_Oriented_Programming:
    
    class Inheritance():
        def __init__(self, name, age):
            self.name = name
            self.age = age

        class SchoolMember():
            '''Represents any school member.'''

            def __init__(self, name, age):
                self.name = name
                self.age = age
                print('(Initialized SchoolMember: {})'.format(self.name))

            def tell(self):
                '''Tell my details.'''
                print('Name:"{}" Age:"{}"'.format(
                    self.name, self.age), end=" ")

        class Teacher(SchoolMember):
            '''Represents a teacher.'''

            def __init__(self, name, age, salary):
                Object_Oriented_Programming.Inheritance.SchoolMember.__init__(
                    self, name, age)
                self.salary = salary
                print('(Initialized Teacher: {})'.format(self.name))

            def tell(self):
                Object_Oriented_Programming.Inheritance.SchoolMember.tell(self)
                print('Salary: "{:d}"'.format(self.salary))

hỏi ngày 21 tháng 2 lúc 17:08Feb 21 at 17:08

Hướng dẫn how do you define names in python 3? - làm thế nào để bạn xác định tên trong python 3?

4

Theo như tôi quan tâm về mã, tôi đã chạy nó trên thiết bị đầu cuối và không có vấn đề gì, nó được thực hiện đơn giản và không có gì hiển thị vì không có gì được in.

Bây giờ, nếu bạn thực sự muốn biết cách xác định một đối tượng trong OOPS trong Python, thì đây là cách để làm điều đó:

  1. Đầu tiên thay vì tạo một lớp bên trong một lớp, bạn chỉ nên tạo một lớp và sau đó viết phương thức
    def hello():
        print("Hello, World!")
    
    hello()
    
    
    2 bao gồm tất cả những thứ như tên hoặc tuổi.
  2. Khi nào với điều này, sau đó bạn có thể tạo một biến mới ở cuối và đặt nó = tên lớp (tên, tuổi). Ví dụ, tôi đã đính kèm một IMG để hiển thị cho bạn một đoạn mã.

Mã số

Tomerikoo

16.7K15 Huy hiệu vàng38 Huy hiệu bạc55 Huy hiệu Đồng15 gold badges38 silver badges55 bronze badges

Đã trả lời ngày 21 tháng 2 lúc 17:36Feb 21 at 17:36

Hướng dẫn how do you define names in python 3? - làm thế nào để bạn xác định tên trong python 3?

5

mã số :

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()

Đã trả lời ngày 22 tháng 2 lúc 11:20Feb 22 at 11:20

Hướng dẫn how do you define names in python 3? - làm thế nào để bạn xác định tên trong python 3?

1

Giới thiệu

Một chức năng là một khối các hướng dẫn thực hiện một hành động và, một khi được xác định, có thể được sử dụng lại. Các chức năng làm cho mã trở nên mô -đun hơn, cho phép bạn sử dụng cùng một mã nhiều lần.function is a block of instructions that performs an action and, once defined, can be reused. Functions make code more modular, allowing you to use the same code over and over again.

Python có một số chức năng tích hợp mà bạn có thể quen thuộc, bao gồm:

  • def hello():
        print("Hello, World!")
    
    hello()
    
    
    3 sẽ in một đối tượng vào thiết bị đầu cuối
  • def hello():
        print("Hello, World!")
    
    hello()
    
    
    4 sẽ chuyển đổi loại dữ liệu chuỗi hoặc số thành kiểu dữ liệu số nguyên
  • def hello():
        print("Hello, World!")
    
    hello()
    
    
    5 trả về chiều dài của một đối tượng

Tên chức năng bao gồm dấu ngoặc đơn và có thể bao gồm các tham số.

Trong hướng dẫn này, chúng tôi sẽ đi qua cách xác định các chức năng của riêng bạn để sử dụng trong các dự án mã hóa của bạn.

Điều kiện tiên quyết

Bạn nên cài đặt Python 3 và môi trường lập trình được thiết lập trên máy tính hoặc máy chủ của bạn. Nếu bạn không có môi trường lập trình được thiết lập, bạn có thể tham khảo hướng dẫn cài đặt và thiết lập cho môi trường lập trình cục bộ hoặc cho môi trường lập trình trên máy chủ của bạn phù hợp với hệ điều hành của bạn (Ubuntu, Centos, Debian, v.v.)

Xác định một chức năng

Hãy để bắt đầu với việc biến đổi cổ điển Hello Hello, thế giới! chương trình thành một chức năng.

Chúng tôi sẽ tạo một tệp văn bản mới trong trình soạn thảo văn bản của chúng tôi và gọi chương trình

def hello():
    print("Hello, World!")

hello()

6. Sau đó, chúng tôi sẽ xác định chức năng.

Một hàm được xác định bằng cách sử dụng từ khóa

def hello():
    print("Hello, World!")

hello()

7, theo sau là tên bạn chọn, theo sau là một tập hợp dấu ngoặc đơn chứa bất kỳ tham số nào mà hàm sẽ lấy (chúng có thể trống) và kết thúc bằng dấu hai chấm.

Thông tin: Để làm theo cùng với mã ví dụ trong hướng dẫn này, hãy mở một vỏ tương tác Python trên hệ thống cục bộ của bạn bằng cách chạy lệnh

def hello():
    print("Hello, World!")

hello()

8. Sau đó, bạn có thể sao chép, dán hoặc chỉnh sửa các ví dụ bằng cách thêm chúng sau dấu nhắc
def hello():
    print("Hello, World!")

hello()

9.
To follow along with the example code in this tutorial, open a Python interactive shell on your local system by running the
def hello():
    print("Hello, World!")

hello()

8 command. Then you can copy, paste, or edit the examples by adding them after the
def hello():
    print("Hello, World!")

hello()

9 prompt.

Trong trường hợp này, chúng tôi sẽ xác định một hàm có tên

  1. python hello.py
0:

hello.py

def hello():

Điều này thiết lập câu lệnh ban đầu để tạo một hàm.

Từ đây, chúng tôi sẽ thêm một dòng thứ hai với thụt 4 không gian để cung cấp các hướng dẫn cho những gì chức năng làm. Trong trường hợp này, chúng tôi sẽ in

  1. python hello.py
1 vào bảng điều khiển:

hello.py

def hello():
    print("Hello, World!")

Chức năng của chúng tôi hiện được xác định đầy đủ, nhưng nếu chúng tôi chạy chương trình vào thời điểm này, sẽ không có gì xảy ra vì chúng tôi đã gọi chức năng.

Vì vậy, bên ngoài khối chức năng được xác định của chúng tôi, hãy để Gọi chức năng với

  1. python hello.py
0:

hello.py

def hello():
    print("Hello, World!")

hello()

Bây giờ, hãy để chạy chương trình:

  1. python hello.py

Bạn sẽ nhận được đầu ra sau:

Output

Hello, World!

Các chức năng có thể phức tạp hơn hàm

  1. python hello.py
0 mà chúng tôi đã xác định ở trên. Ví dụ: chúng ta có thể sử dụng các vòng
  1. python hello.py
4, các câu lệnh có điều kiện và nhiều hơn trong khối chức năng của chúng ta.

Ví dụ: hàm được xác định dưới đây sử dụng một câu lệnh có điều kiện để kiểm tra xem đầu vào cho biến

  1. python hello.py
5 có chứa nguyên âm hay không, sau đó sử dụng vòng lặp
  1. python hello.py
4 để lặp lại các chữ cái trong chuỗi
  1. python hello.py
5.

names.py

# Define function names()
def names():
    # Set up name variable with input
    name = str(input('Enter your name: '))
    # Check whether name has a vowel
    if set('aeiou').intersection(name.lower()):
        print('Your name contains a vowel.')
    else:
        print('Your name does not contain a vowel.')

    # Iterate over name
    for letter in name:
        print(letter)

# Call the function
names()

Hàm

  1. python hello.py
8 mà chúng tôi đã xác định ở trên đặt ra một câu lệnh có điều kiện và vòng lặp
  1. python hello.py
4, cho thấy cách sắp xếp mã trong một định nghĩa hàm. Tuy nhiên, tùy thuộc vào những gì chúng tôi dự định với chương trình của chúng tôi và cách chúng tôi muốn thiết lập mã của mình, chúng tôi có thể muốn xác định câu lệnh có điều kiện và vòng lặp
  1. python hello.py
4 là hai chức năng riêng biệt.

Xác định các chức năng trong một chương trình làm cho mô -đun mã của chúng tôi và có thể tái sử dụng để chúng tôi có thể gọi các chức năng tương tự mà không cần viết lại chúng.

Làm việc với các tham số

Cho đến nay chúng tôi đã xem xét các chức năng với dấu ngoặc đơn không có đối số, nhưng chúng tôi có thể xác định các tham số trong các định nghĩa chức năng trong dấu ngoặc đơn của chúng.

Một tham số là một thực thể được đặt tên trong một định nghĩa hàm, chỉ định một đối số mà hàm có thể chấp nhận.parameter is a named entity in a function definition, specifying an argument that the function can accept.

Hãy để tạo ra một chương trình nhỏ có trong các tham số

Output

Hello, World!
1,

Output

Hello, World!
2 và

Output

Hello, World!
3. Chúng tôi sẽ tạo một chức năng thêm các tham số với nhau trong các cấu hình khác nhau. Các tổng của chúng sẽ được in bởi hàm. Sau đó, chúng tôi sẽ gọi hàm và chuyển số vào hàm.

add_numbers.py

def add_numbers(x, y, z):
    a = x + y
    b = x + z
    c = y + z
    print(a, b, c)

add_numbers(1, 2, 3)

Chúng tôi đã vượt qua số

Output

Hello, World!
4 trong tham số

Output

Hello, World!
1,

Output

Hello, World!
6 trong tham số

Output

Hello, World!
2 và

Output

Hello, World!
8 trong tham số

Output

Hello, World!
3. Các giá trị này tương ứng với từng tham số theo thứ tự chúng được đưa ra.

Chương trình về cơ bản đang thực hiện toán học sau dựa trên các giá trị mà chúng tôi đã truyền cho các tham số:

a = 1 + 2
b = 1 + 3
c = 2 + 3

Hàm cũng in

# Define function names()
def names():
    # Set up name variable with input
    name = str(input('Enter your name: '))
    # Check whether name has a vowel
    if set('aeiou').intersection(name.lower()):
        print('Your name contains a vowel.')
    else:
        print('Your name does not contain a vowel.')

    # Iterate over name
    for letter in name:
        print(letter)

# Call the function
names()

0,
# Define function names()
def names():
    # Set up name variable with input
    name = str(input('Enter your name: '))
    # Check whether name has a vowel
    if set('aeiou').intersection(name.lower()):
        print('Your name contains a vowel.')
    else:
        print('Your name does not contain a vowel.')

    # Iterate over name
    for letter in name:
        print(letter)

# Call the function
names()

1 và
# Define function names()
def names():
    # Set up name variable with input
    name = str(input('Enter your name: '))
    # Check whether name has a vowel
    if set('aeiou').intersection(name.lower()):
        print('Your name contains a vowel.')
    else:
        print('Your name does not contain a vowel.')

    # Iterate over name
    for letter in name:
        print(letter)

# Call the function
names()

2 và dựa trên toán học trên, chúng tôi mong đợi
# Define function names()
def names():
    # Set up name variable with input
    name = str(input('Enter your name: '))
    # Check whether name has a vowel
    if set('aeiou').intersection(name.lower()):
        print('Your name contains a vowel.')
    else:
        print('Your name does not contain a vowel.')

    # Iterate over name
    for letter in name:
        print(letter)

# Call the function
names()

0 sẽ bằng

Output

Hello, World!
8,
# Define function names()
def names():
    # Set up name variable with input
    name = str(input('Enter your name: '))
    # Check whether name has a vowel
    if set('aeiou').intersection(name.lower()):
        print('Your name contains a vowel.')
    else:
        print('Your name does not contain a vowel.')

    # Iterate over name
    for letter in name:
        print(letter)

# Call the function
names()

1 là
# Define function names()
def names():
    # Set up name variable with input
    name = str(input('Enter your name: '))
    # Check whether name has a vowel
    if set('aeiou').intersection(name.lower()):
        print('Your name contains a vowel.')
    else:
        print('Your name does not contain a vowel.')

    # Iterate over name
    for letter in name:
        print(letter)

# Call the function
names()

6 và
# Define function names()
def names():
    # Set up name variable with input
    name = str(input('Enter your name: '))
    # Check whether name has a vowel
    if set('aeiou').intersection(name.lower()):
        print('Your name contains a vowel.')
    else:
        print('Your name does not contain a vowel.')

    # Iterate over name
    for letter in name:
        print(letter)

# Call the function
names()

2 là
# Define function names()
def names():
    # Set up name variable with input
    name = str(input('Enter your name: '))
    # Check whether name has a vowel
    if set('aeiou').intersection(name.lower()):
        print('Your name contains a vowel.')
    else:
        print('Your name does not contain a vowel.')

    # Iterate over name
    for letter in name:
        print(letter)

# Call the function
names()

8. Hãy để chạy chương trình:

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
0
        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
1

Khi chúng tôi vượt qua

Output

Hello, World!
4,

Output

Hello, World!
6 và

Output

Hello, World!
8 làm tham số cho hàm
def add_numbers(x, y, z):
    a = x + y
    b = x + z
    c = y + z
    print(a, b, c)

add_numbers(1, 2, 3)

2, chúng tôi sẽ nhận được đầu ra dự kiến.

Các tham số là các đối số thường được định nghĩa là các biến trong các định nghĩa hàm. Chúng có thể được gán các giá trị khi bạn chạy phương thức, chuyển các đối số vào hàm.

Từ khóa đối số

Ngoài các tham số gọi theo thứ tự, bạn có thể sử dụng các đối số từ khóa trong lệnh gọi hàm, trong đó người gọi xác định các đối số bằng tên tham số.keyword arguments in a function call, in which the caller identifies the arguments by the parameter name.

Khi bạn sử dụng các đối số từ khóa, bạn có thể sử dụng các tham số theo thứ tự vì trình thông dịch Python sẽ sử dụng các từ khóa được cung cấp để phù hợp với các giá trị với các tham số.

Hãy để tạo ra một chức năng sẽ hiển thị thông tin hồ sơ cho người dùng. Chúng tôi sẽ chuyển các tham số cho nó dưới dạng

def add_numbers(x, y, z):
    a = x + y
    b = x + z
    c = y + z
    print(a, b, c)

add_numbers(1, 2, 3)

3 (dự định dưới dạng chuỗi) và
def add_numbers(x, y, z):
    a = x + y
    b = x + z
    c = y + z
    print(a, b, c)

add_numbers(1, 2, 3)

4 (dự định là một số nguyên).

profile.py

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
2

Trong câu lệnh định nghĩa hàm,

def add_numbers(x, y, z):
    a = x + y
    b = x + z
    c = y + z
    print(a, b, c)

add_numbers(1, 2, 3)

3 và
def add_numbers(x, y, z):
    a = x + y
    b = x + z
    c = y + z
    print(a, b, c)

add_numbers(1, 2, 3)

4 được chứa trong các dấu ngoặc đơn của hàm
def add_numbers(x, y, z):
    a = x + y
    b = x + z
    c = y + z
    print(a, b, c)

add_numbers(1, 2, 3)

7. Khối của hàm in thông tin về người dùng dưới dạng chuỗi, sử dụng hai tham số.

Bây giờ, chúng ta có thể gọi chức năng và gán các tham số cho nó:

profile.py

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
3

Trong cuộc gọi chức năng đầu tiên, chúng tôi đã điền thông tin với tên người dùng là

def add_numbers(x, y, z):
    a = x + y
    b = x + z
    c = y + z
    print(a, b, c)

add_numbers(1, 2, 3)

8 và người theo dõi là
def add_numbers(x, y, z):
    a = x + y
    b = x + z
    c = y + z
    print(a, b, c)

add_numbers(1, 2, 3)

9, trong chức năng thứ hai, chúng tôi đã sử dụng các đối số từ khóa, gán các giá trị cho các biến đối số.

Hãy để chạy chương trình:

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
4
        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
5

Đầu ra cho chúng ta thấy tên người dùng và số lượng người theo dõi cho cả hai người dùng.

Điều này cũng cho phép chúng tôi sửa đổi thứ tự của các tham số, như trong ví dụ này của cùng một chương trình với một cuộc gọi khác:

profile.py

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
6

Khi chúng tôi chạy lại chương trình với lệnh

a = 1 + 2
b = 1 + 3
c = 2 + 3
0, chúng tôi sẽ nhận được đầu ra sau:

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
7

Bởi vì định nghĩa hàm duy trì cùng một thứ tự của các câu lệnh

def hello():
    print("Hello, World!")

hello()

3, nếu chúng ta sử dụng các đối số từ khóa, không quan trọng chúng ta sẽ chuyển chúng vào lệnh gọi hàm.

Giá trị đối số mặc định

Chúng tôi cũng có thể cung cấp các giá trị mặc định cho một hoặc cả hai tham số. Hãy để tạo ra một giá trị mặc định cho tham số

def add_numbers(x, y, z):
    a = x + y
    b = x + z
    c = y + z
    print(a, b, c)

add_numbers(1, 2, 3)

4 với giá trị

Output

Hello, World!
4:

profile.py

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
8

Bây giờ, chúng ta có thể chạy chức năng chỉ với hàm tên người dùng được gán và số lượng người theo dõi sẽ tự động mặc định thành 1. Chúng ta vẫn có thể thay đổi số lượng người theo dõi nếu chúng ta muốn.

profile.py

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
9

Khi chúng tôi chạy chương trình bằng lệnh

a = 1 + 2
b = 1 + 3
c = 2 + 3
0, chúng tôi sẽ nhận được đầu ra sau:

def hello():
0

Cung cấp các tham số mặc định với các giá trị có thể cho phép chúng tôi bỏ qua các giá trị xác định cho mỗi đối số đã có mặc định.

Trả lại một giá trị

Bạn có thể chuyển một giá trị tham số vào một hàm và một hàm cũng có thể tạo ra một giá trị.

Một hàm có thể tạo ra một giá trị với câu lệnh

a = 1 + 2
b = 1 + 3
c = 2 + 3
5, sẽ thoát một hàm và tùy chọn chuyển một biểu thức trở lại cho người gọi. Nếu bạn sử dụng câu lệnh
a = 1 + 2
b = 1 + 3
c = 2 + 3
5 không có đối số, hàm sẽ trả về
a = 1 + 2
b = 1 + 3
c = 2 + 3
7.

Cho đến nay, chúng tôi đã sử dụng câu lệnh

def hello():
    print("Hello, World!")

hello()

3 thay vì tuyên bố
a = 1 + 2
b = 1 + 3
c = 2 + 3
5 trong các chức năng của chúng tôi. Hãy để tạo ra một chương trình mà thay vì in sẽ trả về một biến.

Trong một tệp văn bản mới có tên

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
00, chúng tôi sẽ tạo một chương trình bình phương tham số

Output

Hello, World!
1 và trả về biến

Output

Hello, World!
2. Chúng tôi đưa ra một cuộc gọi để in biến
        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
03, được hình thành bằng cách chạy chức năng
        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
04 với

Output

Hello, World!
8 được truyền vào đó.

square.py

def hello():
1

Chúng tôi có thể chạy chương trình và nhận đầu ra:

def hello():
2
def hello():
3

Số nguyên

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
06 được trả về làm đầu ra, đó là những gì chúng ta mong đợi bằng cách yêu cầu Python tìm thấy bình phương 3.

Để hiểu thêm về cách thức tuyên bố

a = 1 + 2
b = 1 + 3
c = 2 + 3
5, chúng ta có thể nhận xét câu lệnh
a = 1 + 2
b = 1 + 3
c = 2 + 3
5 trong chương trình:

square.py

def hello():
4

Bây giờ, hãy để Lừa điều hành chương trình một lần nữa:

def hello():
2
def hello():
6

Không sử dụng câu lệnh

a = 1 + 2
b = 1 + 3
c = 2 + 3
5 ở đây, chương trình không thể trả về giá trị để giá trị mặc định thành
a = 1 + 2
b = 1 + 3
c = 2 + 3
7.

Một ví dụ khác, trong chương trình

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
11 ở trên, chúng ta có thể trao đổi câu lệnh
def hello():
    print("Hello, World!")

hello()

3 cho câu lệnh
a = 1 + 2
b = 1 + 3
c = 2 + 3
5.

add_numbers.py

def hello():
7

Bên ngoài hàm, chúng tôi đặt biến

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
14 bằng kết quả của hàm lấy trong

Output

Hello, World!
4,

Output

Hello, World!
6 và

Output

Hello, World!
8 như chúng tôi đã làm ở trên. Sau đó, chúng tôi gọi một bản in của biến
        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
14.

Bây giờ, hãy để chạy lại chương trình mà nó có câu lệnh

a = 1 + 2
b = 1 + 3
c = 2 + 3
5:

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
0
def hello():
9

Chúng tôi nhận được cùng một số

Output

Hello, World!
8,
# Define function names()
def names():
    # Set up name variable with input
    name = str(input('Enter your name: '))
    # Check whether name has a vowel
    if set('aeiou').intersection(name.lower()):
        print('Your name contains a vowel.')
    else:
        print('Your name does not contain a vowel.')

    # Iterate over name
    for letter in name:
        print(letter)

# Call the function
names()

6 và
# Define function names()
def names():
    # Set up name variable with input
    name = str(input('Enter your name: '))
    # Check whether name has a vowel
    if set('aeiou').intersection(name.lower()):
        print('Your name contains a vowel.')
    else:
        print('Your name does not contain a vowel.')

    # Iterate over name
    for letter in name:
        print(letter)

# Call the function
names()

8 dưới dạng đầu ra mà chúng tôi nhận được trước đây bằng cách sử dụng câu lệnh
def hello():
    print("Hello, World!")

hello()

3 trong hàm. Lần này, nó được phân phối dưới dạng một bộ xử lý vì danh sách biểu thức tuyên bố ____95 có ít nhất một dấu phẩy.

Các chức năng thoát ngay lập tức khi họ đạt được câu lệnh

a = 1 + 2
b = 1 + 3
c = 2 + 3
5, cho dù họ có trả lại giá trị hay không.

return_loop.py

def hello():
    print("Hello, World!")

0

Sử dụng câu lệnh

a = 1 + 2
b = 1 + 3
c = 2 + 3
5 trong vòng
  1. python hello.py
4 kết thúc hàm, do đó, dòng nằm ngoài vòng lặp sẽ không chạy. Nếu, thay vào đó, chúng tôi đã sử dụng một câu lệnh
        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
28, chỉ có vòng lặp sẽ thoát ra vào thời điểm đó và dòng
def hello():
    print("Hello, World!")

hello()

3 cuối cùng sẽ chạy.

Câu lệnh

a = 1 + 2
b = 1 + 3
c = 2 + 3
5 thoát ra một hàm và có thể trả về một giá trị khi được phát hành với một tham số.

Sử dụng class train: def __init__(self, name, fare, seats, code): self.name = name self.fare = fare self.seats = seats self.code = code def train_Status(self): print(f"The name of the train is {self.name}") print(f"The seats is {self. seats}") def fare_Info(self): print(f"The fare is {self. fare}") def code_Info(self): print(f"The code is {self. code}") def tickets_Info(self): if(self.seats > 0): print( f"The seats are available for you...\nYour seat number is {self.seats}") self.seats = self.seats - 1 elif(self.seats == 0): print("The seats are not available for you...") else: print("The server isnt updated yet. \nPlease try again later.") @staticmethod def greeting(): print("Welcome to Rajdhani express!!") Inter = train("Inter Express", 180, 12, 239340) Inter.greeting() Inter.fare_Info() Inter.train_Status() Inter.tickets_Info() Inter.train_Status() Inter.code_Info() 31 làm hàm

Mặc dù trong Python, bạn có thể gọi hàm ở cuối chương trình của mình và nó sẽ chạy (như chúng tôi đã thực hiện trong các ví dụ trên), nhiều ngôn ngữ lập trình (như C ++ và Java) yêu cầu hàm

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
32 để thực thi. Bao gồm hàm
        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
31, mặc dù không bắt buộc, có thể cấu trúc các chương trình Python của chúng tôi một cách hợp lý đặt các thành phần quan trọng nhất của chương trình vào một hàm. Nó cũng có thể làm cho các chương trình của chúng tôi dễ dàng hơn cho các lập trình viên không phải là Python để đọc.

Chúng tôi sẽ bắt đầu với việc thêm chức năng

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
31 vào chương trình
def hello():
    print("Hello, World!")

hello()

6 ở trên. Chúng tôi sẽ giữ chức năng
  1. python hello.py
0 của chúng tôi và sau đó xác định hàm
        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
31:

hello.py

def hello():
    print("Hello, World!")

1

Trong hàm

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
31, hãy để Lôi bao gồm một câu lệnh
def hello():
    print("Hello, World!")

hello()

3 để cho chúng tôi biết rằng chúng tôi đã làm trong hàm
        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
31. Ngoài ra, hãy để cho gọi chức năng
  1. python hello.py
0 trong hàm
        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
31:

hello.py

def hello():
    print("Hello, World!")

2

Cuối cùng, ở cuối chương trình, chúng tôi sẽ gọi hàm

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
31:

hello.py

def hello():
    print("Hello, World!")

3

Tại thời điểm này, chúng tôi có thể chạy chương trình của mình:

  1. python hello.py

Chúng tôi sẽ nhận được đầu ra sau:

def hello():
    print("Hello, World!")

5

Bởi vì chúng tôi đã gọi hàm

  1. python hello.py
0 trong
        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
31 và sau đó chỉ gọi
        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
31 để chạy, văn bản
  1. python hello.py
1 chỉ được in một lần, sau khi chuỗi nói với chúng tôi rằng chúng tôi đang ở trong hàm chính.

Tiếp theo, chúng tôi sẽ làm việc với nhiều chức năng, vì vậy nó đáng để xem xét phạm vi biến của các biến toàn cầu và địa phương. Nếu bạn xác định một biến trong một khối hàm, bạn sẽ chỉ có thể sử dụng biến đó trong hàm đó. Nếu bạn muốn sử dụng các biến trên các chức năng, có thể tốt hơn là khai báo một biến toàn cầu.

Trong Python,

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
48 là tên của phạm vi nơi mã cấp cao sẽ thực thi. Khi một chương trình được chạy từ đầu vào tiêu chuẩn, tập lệnh hoặc từ một lời nhắc tương tác,
        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
49 của nó được đặt bằng
        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
48.

Bởi vì điều này, có một quy ước để sử dụng công trình sau:

def hello():
    print("Hello, World!")

6

Điều này cho phép các tệp chương trình được sử dụng:

  • Là chương trình chính và chạy những gì tiếp theo câu lệnh
            class train:
        def __init__(self, name, fare, seats, code):
            self.name = name
            self.fare = fare
            self.seats = seats
            self.code = code
    
        def train_Status(self):
            print(f"The name of the train is {self.name}")
            print(f"The seats is {self. seats}")
    
        def fare_Info(self):
            print(f"The fare is {self. fare}")
    
        def code_Info(self):
            print(f"The code is {self. code}")
    
        def tickets_Info(self):
            if(self.seats > 0):
                print(
                    f"The seats are available for you...\nYour seat number is {self.seats}")
                self.seats = self.seats - 1
            elif(self.seats == 0):
                print("The seats are not available for you...")
            else:
                print("The server isnt updated yet. \nPlease try again later.")
    
        @staticmethod
        def greeting():
            print("Welcome to Rajdhani express!!")
    
    
    Inter = train("Inter Express", 180, 12, 239340)
    Inter.greeting()
    Inter.fare_Info()
    Inter.train_Status()
    Inter.tickets_Info()
    Inter.train_Status()
    Inter.code_Info()
    
    51
  • Là một mô -đun và không chạy những gì tiếp theo câu lệnh
            class train:
        def __init__(self, name, fare, seats, code):
            self.name = name
            self.fare = fare
            self.seats = seats
            self.code = code
    
        def train_Status(self):
            print(f"The name of the train is {self.name}")
            print(f"The seats is {self. seats}")
    
        def fare_Info(self):
            print(f"The fare is {self. fare}")
    
        def code_Info(self):
            print(f"The code is {self. code}")
    
        def tickets_Info(self):
            if(self.seats > 0):
                print(
                    f"The seats are available for you...\nYour seat number is {self.seats}")
                self.seats = self.seats - 1
            elif(self.seats == 0):
                print("The seats are not available for you...")
            else:
                print("The server isnt updated yet. \nPlease try again later.")
    
        @staticmethod
        def greeting():
            print("Welcome to Rajdhani express!!")
    
    
    Inter = train("Inter Express", 180, 12, 239340)
    Inter.greeting()
    Inter.fare_Info()
    Inter.train_Status()
    Inter.tickets_Info()
    Inter.train_Status()
    Inter.code_Info()
    
    51.

Bất kỳ mã nào không có trong câu lệnh này sẽ được thực thi khi chạy. Nếu bạn sử dụng tệp chương trình của mình làm mô -đun, mã không có trong câu lệnh này cũng sẽ thực thi khi nhập trong khi chạy tệp thứ cấp.

Hãy để mở rộng trên chương trình

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
53 của chúng tôi ở trên và tạo một tệp mới có tên
        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
54. Trong chương trình này, chúng tôi sẽ khai báo một biến toàn cầu và sửa đổi chức năng
  1. python hello.py
8 ban đầu của chúng tôi để các hướng dẫn nằm trong hai chức năng riêng biệt.

Hàm đầu tiên,

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
56 sẽ kiểm tra xem chuỗi
  1. python hello.py
5 có chứa nguyên âm không.

Hàm thứ hai

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
58 sẽ in từng chữ cái của chuỗi
  1. python hello.py
5.

more_names.py

def hello():
    print("Hello, World!")

7

Với thiết lập này, hãy để xác định chức năng

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
31 sẽ chứa một cuộc gọi đến cả hai hàm
        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
56 và
        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
58.

more_names.py

def hello():
    print("Hello, World!")

8

Cuối cùng, chúng tôi sẽ thêm cấu trúc

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
63 ở cuối tệp. Đối với mục đích của chúng tôi, vì chúng tôi đã đặt tất cả các chức năng mà chúng tôi muốn thực hiện trong hàm
        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
31, chúng tôi sẽ gọi hàm
        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
31 theo câu lệnh
        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
51 này.

more_names.py

def hello():
    print("Hello, World!")

9

Bây giờ chúng ta có thể chạy chương trình:

def hello():
    print("Hello, World!")

hello()

0

Chương trình sẽ hiển thị đầu ra giống như chương trình

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
53, nhưng ở đây mã được tổ chức nhiều hơn và có thể được sử dụng theo cách mô -đun mà không cần sửa đổi.

Nếu bạn không muốn khai báo chức năng

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
31, bạn có thể kết thúc chương trình như thế này:

more_names.py

def hello():
    print("Hello, World!")

hello()

1

Sử dụng

        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
31 làm hàm và câu lệnh
        class train:
    def __init__(self, name, fare, seats, code):
        self.name = name
        self.fare = fare
        self.seats = seats
        self.code = code

    def train_Status(self):
        print(f"The name of the train is {self.name}")
        print(f"The seats is {self. seats}")

    def fare_Info(self):
        print(f"The fare is {self. fare}")

    def code_Info(self):
        print(f"The code is {self. code}")

    def tickets_Info(self):
        if(self.seats > 0):
            print(
                f"The seats are available for you...\nYour seat number is {self.seats}")
            self.seats = self.seats - 1
        elif(self.seats == 0):
            print("The seats are not available for you...")
        else:
            print("The server isnt updated yet. \nPlease try again later.")

    @staticmethod
    def greeting():
        print("Welcome to Rajdhani express!!")


Inter = train("Inter Express", 180, 12, 239340)
Inter.greeting()
Inter.fare_Info()
Inter.train_Status()
Inter.tickets_Info()
Inter.train_Status()
Inter.code_Info()
63 có thể sắp xếp mã của bạn một cách hợp lý, làm cho nó dễ đọc và mô -đun hơn.

Sự kết luận

Các chức năng là các khối mã của các hướng dẫn thực hiện các hành động trong một chương trình, giúp làm cho mã của chúng tôi có thể tái sử dụng và mô -đun.

Để tìm hiểu thêm về cách làm cho mã của bạn trở nên mô -đun hơn, bạn có thể đọc hướng dẫn của chúng tôi về cách viết các mô -đun trong Python 3.

Làm thế nào để bạn xác định một cái gì đó trong Python 3?

Một hàm được xác định bằng cách sử dụng từ khóa DEF, theo sau là tên bạn chọn, theo sau là một tập hợp dấu ngoặc đơn chứa bất kỳ tham số nào mà hàm sẽ lấy (chúng có thể trống) và kết thúc bằng dấu hai chấm.using the def keyword, followed by a name of your choosing, followed by a set of parentheses which hold any parameters the function will take (they can be empty), and ending with a colon.

Làm thế nào để bạn xác định một tên biến?

Một biến là một tên biểu tượng cho (hoặc tham chiếu đến) thông tin.Tên của biến thể hiện thông tin mà biến chứa.Chúng được gọi là các biến vì thông tin được đại diện có thể thay đổi nhưng các hoạt động trên biến vẫn giữ nguyên.a symbolic name for (or reference to) information. The variable's name represents what information the variable contains. They are called variables because the represented information can change but the operations on the variable remain the same.

Làm thế nào để bạn sử dụng hàm tên trong Python?

Bốn bước để xác định một hàm trong Python là như sau: Sử dụng từ khóa def để khai báo chức năng và theo dõi tên này với tên chức năng.Thêm tham số vào hàm: Chúng nên nằm trong dấu ngoặc đơn của hàm.Kết thúc dòng của bạn bằng một dấu hai chấm.Use the keyword def to declare the function and follow this up with the function name. Add parameters to the function: they should be within the parentheses of the function. End your line with a colon.

3 quy tắc để đặt tên một biến là gì?

Quy tắc đặt tên biến: Một tên biến phải bắt đầu bằng chữ cái hoặc ký tự dấu gạch dưới (_) Một tên biến không thể bắt đầu bằng một chữ số.Một tên biến chỉ có thể chứa các ký tự alpha-numeric và nhấn mạnh (A-Z, A-Z, 0-9 và _)A variable name must start with a letter or an underscore character (_) A variable name cannot start with a digit. A variable name can only contain alpha-numeric characters and underscores ( a-z, A-Z , 0-9 , and _ )