Click here to Skip to main content
15,917,642 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
import random # need to import random to have use random in number

# fill your information here
stud_id = "" 
stud_name = ""

class base_class:
    '''
    base class, will use a template for children classes; will not be used for creating objects
    '''
    def __init__(self,ipadd):
        self.ip_address = ipadd
        self.y =self.ip_address.split(".")
    '''   
    def ipclass(self):
        y =self.ip_address.split(".")   
        y=int(y[0])
        if y>=1 and y<=126:
            print("Class A")
            
        elif y>=128 and y<=191:                
             print("Class B")
             
             
     '''        
    
    def Print(self):
        
        print(self.ip_address)
        
class Class_A(base_class):
    def ipclass(self):
        #y =self.ip_address.split(".")   
        self.y=int(self.y[0])
        if self.y>=1 and self.y<=126:  
            print("This is ClassA Address")
        else:
            print("loop")
    pass
    
      
   # def __init__(self,ip):
       # self.ip_address=ip
          
           
#obj = Class_A("10.10.10.10")
        
#obj.print()
#obj.ipclass()
            
        
#pass


'''
make sure that the 5 children class are 
  
        

    use base class for Class A IP address
    Use base class and change what needs to be change for class A 
    '''
    
        
class Class_B(base_class):
    
    def ipclass(self):
        #y =self.ip_address.split(".")   
        self.y=int(self.y[0])
        if self.y>=128 and self.y<=191: 
            print("This is Class B Address")
        else:
            print("loop")
    pass
    
    #def __init__(self,ip):
       # self.ip_address=ip


#obj1 = Class_B("129.10.10.10")


#obj1.ipclass()    
#pass
    
'''
    use base class for Class B IP address
    Use base class and change what needs to be change for class B
  '''
    

class Class_C(base_class):
    def ipclass(self):
        #y =self.ip_address.split(".")   
        self.y=int(self.y[0])
        if self.y>=192 and self.y<=223: 
            print("This is Class C Address")
        else:
            print("loop")
    pass    
    
    '''
    use base class for Class C IP address
    Use base class and change what needs to be change for class C
    '''
    pass 
        
class Class_LoopBack(base_class):
    
    def ipclass(self):
        #y =self.ip_address.split(".")   
        self.y=int(self.y[0])
        if self.y>=127 and self.y<=128: 
            print("This is Class Loopback Address")
        else:
            print("loop")
    pass        
    '''
    use base class for Loopback IP address
    Use base class and change what needs to be change for loopback
    '''
    pass   


class Classes_D_and_E(base_class):
    
    def ipclass(self):
        #y =self.ip_address.split(".")   
        self.y=int(self.y[0])
        if self.y>=224 and self.y<=255: 
            print("This is Classes_D_and_E")
        else:
            print("loop")
    pass            
    '''
    use base classs for Class D, class E, and non ip address
    Use base class and change what needs to be change for class D, E, and non IP Address
    '''
    pass
def function(obj):
    obj.ipclass()
    obj.Print()  

A = Class_A("10.10.10.10")
function(A)
'''
obj.Print()
obj.ipclass()
'''
B= Class_B("129.10.10.10")
function(B)
'''
.Print()
obj1.ipclass()
'''
C= Class_C("192.10.10.10")
function(C)
'''
.Print()
obj1.ipclass()
'''

LoopBack= Class_LoopBack("127.255.255.255")
function(LoopBack)
'''
.Print()
obj1.ipclass()
'''

D_E= Classes_D_and_E("224.10.10.10")
function(D_E)
'''
.Print()
obj1.ipclass()
'''


def ip_class(ip):
    
    '''
    Function takes in a ip address in string format. 
    Returns which class the IP address belongs too. 
    IP address 8.8.8.8 is Class A

    print which class the IP address belongs to
    First Octet:
    class A: 1 to 126
    loopback:127
    clsss B: 128 to 191
    class C: 192 to 223
    '''
    
class base_class: 
        
        def _init_(self,ip,class_address="A"):
            self.ip_address = ip
            self.class_address=class_address
            
            def ip_class(self):
                octet= self.ip_address.split(".")
                octet=int(octet[0])
                if octet>=1 and octet<= 126:
                    self.class_address='ClassA: 1 to 126'
                    
                elif octet == 127:
                    self.class_address='loopback:'
                      
                elif octet >= 128 and octet <=191:
                    self.class_address='ClassB: 128 to 191'
                          
                elif octet >= 192 and octet <=223:
                    self.class_address='ClassC: 192 to 223'                          
                elif octet >= 224 and octet <=239:
                    self.class_address='ClassC: 224 to 239'
                          
                elif octet >= 240 and octet <=255:
                    self.class_address='ClassD: 240 to 255'  
                    return "First Octet:  \n"  +self.class_address
                      
class Class_A(base_class):
        
        def _init_(self,ip):
            self.ip_address=ip
            obj =Class_A("8.8.8.8")
            odj.ip_class()
            print(obj.ip_class())
           
class Class_loop(base_class):

        def _init_(self,ip):
            self.ip_address=ip
            obj =Classloop("127.0.0.1")
            odj.ip_class()
            print(obj.ip_class())
    
class ClassB(base_class):

        def _init_(self,ip):
            self.ip_address=ip
            obj =ClassB("128.8.8.8")
            odj.ip_class()
            print(obj.ip_class())    
    
class ClassC(base_class):

        def _init_(self,ip):
            self.ip_address=ip
            obj =ClassC("192.8.8.8")
            odj.ip_class()
            print(obj.ip_class()) 
            
            
        pass
                       
'''
    Function takes in a ip address in string format. 
    Returns which class the IP address belongs too. 
    IP address 8.8.8.8 is Class A
    
    print which class the IP address belongs to
    First Octet:
    class A: 1 to 126
    loopback:127
    clsss B: 128 to 191
    class C: 192 to 223
    
    
    '''
    
def create_randam_IP():
    
    import random
def create_random_IP():
    # create the IP address 
    # generate 4 random numbers in range (0,255)
    oct1 = random.randrange(0,256)
    oct2 = random.randrange(0,256)
    oct3 = random.randrange(0,256)
    oct4 = random.randrange(0,256)
    # create the IP address 
    ip_string = str(oct1) + '.' +str(oct2) + '.' + str(oct3) + '.' + str(oct4)
    # return the ip address
    return ip_string
# call the function to generate IP address
random_IP = create_random_IP()
# display the IP address
print('Randomly generated IP: ', random_IP)

'''
fuction that creates an IP address with random numbers in each Octet and return the created IP address
''' 
    
pass


I haven't get any output for the second program. is my printing method wrong?
Also I am struggling with use inheritance and polymophism for the third program random id
I wrote very simple functional based commant

What I have tried:

I have done many methods which is not worked


program works fine but there is no output in second method: it should get classes according to ip address
Posted
Updated 4-Feb-20 15:42pm
v3
Comments
Richard MacCutchan 4-Feb-20 5:03am    
There is far too much code there to figure out what is going on. You need to reduce it to a simple example: base class plus one child and see what happens.
phil.o 4-Feb-20 5:20am    
You may also define what 'not working' means. Does it produce an error? With a message? Or is it that it does not do what you are expecting? Please use the green Improve question widget which appears on hovering your question, and provide some more details.

1 solution

Quote:
Why print is not working in second program

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900