Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I need a help to write the function of python

+9 votes
asked Jan 30, 2020 by ST_ADIL
def main():
    # using keyword global allows to use the ip_List created outside of function to
    global ip_List
    
    '''
    Use ip_class to append ip_List.
    must check ip address:
        8.8.8.8
        132.216.177.160
        192.168.0.1
        205.211.140.132
        127.0.0.1
    
    use create_randam_IP() to create 1 randomly created ip address
    
    create a for loop that goes through every IP Class in the ip_List
    and then calls the unique Print() function each of the objects
    
    '''
    print("wrtitten by: {0} ID: {1}\n".format(stud_name, stud_id)) #do not change
    

    # write your code for the method here
    # start with IP Address that were given, by adding their classes to ip_List
    # then call the create_randam_IP()
    # add the random IP to ip_List
    
    
    '''
    going through the list and call the unique print method of each class
    '''
    
    for obj in ip_List:
    # calling method
        obj.Print()
        
'''
Calls the main() function
'''
main()

1 Answer

0 votes
answered Jun 27, 2024 by karishma (140 points)
Please check if its help full:

import random

class IPClass:
    def __init__(self, ip):
        self.ip = ip

    def Print(self):
        print(f"IP Address: {self.ip}")

class A(IPClass):
    def __init__(self, ip):
        super().__init__(ip)

    def Print(self):
        print(f"Class A IP Address: {self.ip}")

class B(IPClass):
    def __init__(self, ip):
        super().__init__(ip)

    def Print(self):
        print(f"Class B IP Address: {self.ip}")

class C(IPClass):
    def __init__(self, ip):
        super().__init__(ip)

    def Print(self):
        print(f"Class C IP Address: {self.ip}")

class D(IPClass):
    def __init__(self, ip):
        super().__init__(ip)

    def Print(self):
        print(f"Class D IP Address: {self.ip}")

class E(IPClass):
    def __init__(self, ip):
        super().__init__(ip)

    def Print(self):
        print(f"Class E IP Address: {self.ip}")

def create_random_IP():
    return f"{random.randint(0, 255)}.{random.randint(0, 255)}.{random.randint(0, 255)}.{random.randint(0, 255)}"

def main():
    global ip_List
    ip_List = []
    
    stud_name = "Your Name"
    stud_id = "Your ID"

    print("wrtitten by: {0} ID: {1}\n".format(stud_name, stud_id)) #do not change

    ip_addresses = [
        "8.8.8.8",
        "132.216.177.160",
        "192.168.0.1",
        "205.211.140.132",
        "127.0.0.1"
    ]
    
    def classify_ip(ip):
        first_octet = int(ip.split('.')[0])
        if 0 <= first_octet <= 127:
            return A(ip)
        elif 128 <= first_octet <= 191:
            return B(ip)
        elif 192 <= first_octet <= 223:
            return C(ip)
        elif 224 <= first_octet <= 239:
            return D(ip)
        else:
            return E(ip)

    for ip in ip_addresses:
        ip_class = classify_ip(ip)
        ip_List.append(ip_class)

    random_ip = create_random_IP()
    ip_List.append(classify_ip(random_ip))

    for obj in ip_List:
        obj.Print()

if __name__ == "__main__":
    main()
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...