Почему печать не работает во второй программе
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
программа работает нормально, но во втором методе вывода нет: она должна получать классы по ip-адресу
Richard MacCutchan
Там слишком много кода, чтобы понять, что происходит. Вам нужно свести его к простому примеру: базовый класс плюс один ребенок и посмотреть, что получится.
phil.o
Вы также можете определить, что означает "не работает". Не приводит ли это к ошибке? С сообщением? Или оно не делает того, чего вы ожидаете? Пожалуйста, используйте зеленый цвет Улучшить вопрос виджет, который появляется при наведении курсора на ваш вопрос, и предоставляет еще несколько подробностей.