2017-12-19 12 views
2

주 네트워크 주소가 주어진 서브넷의 수와 주소를 계산하는 스크립트를 작성 중입니다. 나는 2 개의 함수 (현재) - AvailableNetworks()와 BroadcastAddy()를 가지고있다. 이 열을 2 열로 인쇄하여 각 행에 네트워크 ID와 해당 서브넷의 브로드 캐스트 주소가 포함되도록하고 싶습니다. 이렇게하려면 첫 번째 열에 AvailableNetworks()의 출력이 있어야합니다. 두 번째 열은 BroadcastAddy()의 출력을 가져야합니다.한 열에 반복 함수의 출력을 인쇄하고 두 번째 인접 열에 두 번째 반복 함수의 출력을 인쇄하려면 어떻게합니까?

"{:^30} {:^30} {:^30}"으로 .format()를 사용하기위한 최종 목표. 그러나 .format()에는 목록의 목록을 반복하는 데 중대한 문제가있는 것으로 보이거나 적어도 그렇게하는 방법을 알려주는 중요한 문제가 있습니다. 내가 상응하는 인덱스 번호를 가지고 브로드 캐스트 주소의 요소와 Goodnets의 요소를 결합하는() 우편 번호를 사용

MainNetwork = input("What is the main network id address?") 
SubnetsDesired = input("How many subnets do you want to create?") 

GoodNets = [] 
BroadcastAddresses = [] 

def AvailableNetworks(): 
    NetArray = [2, 4, 8, 16, 32, 64, 128, 256] 
    HostArray = [256, 128, 64, 32, 16, 8, 4, 2] 
    for i in NetArray: 
     if i >= int(SubnetsDesired): 
      NumbSubnets = i 
      SubnetIndex = NetArray.index(i) 
      NumIps=HostArray[SubnetIndex + 1] 
      print("Available Networks:") 
      ipaddy = MainNetwork.split(".") 
      ipaddy = list(map(int, ipaddy)) 
      for i in range(NumbSubnets-1): 
       ipaddy[-1] += NumIps 
       GoodNets.append('.'.join(str(i) for i in ipaddy)) 
      break 

def BroadcastAddy(): 
    NetArray = [2, 4, 8, 16, 32, 64, 128, 256] 
    HostArray = [256, 128, 64, 32, 16, 8, 4, 2] 
    for i in NetArray: 
     if i >= int(SubnetsDesired): 
      NumbSubnets = i 
      SubnetIndex = NetArray.index(i) 
      NumIps = HostArray[SubnetIndex + 1] 
      print("Broadcast Adress:") 
      ipaddy = MainNetwork.split(".") 
      ipaddy = list(map(int, ipaddy)) 
      for i in range(NumbSubnets - 1): 
       ipaddy[-1] += NumIps -1 
       BroadcastAddresses.append('.'.join(str(i) for i in ipaddy)) 
       ipaddy[-1] += 1 
      break 

: 여기

내가 작성한 2 개 함수입니다. 내가 할 수있는대로

언급 한 바와 같이
if __name__== '__main__': 
    AvailableNetworks() 
    BroadcastAddy() 

    # This combines lists so 
    FinalReport = zip(GoodNets, BroadcastAddresses) 
    # zip() creates immutable tuples that will give you hell if you try to run them through .format() 
    # So I convert FinalReport back into list of lists 
    FinalReport = [list(elem) for elem in FinalReport] 
    # Bug check (Delete this before final) 
    print("this is the type of final report:", type(FinalReport)) 
    # Bug check, print the FinalReport to see what inside. 
    print(FinalReport) 
    # Formatted, when combined with .format() will create 2 columns. I've printed to column titles 
    # to prove this works. 
    formatted = "{:^30}{:^30}" 
    print(formatted.format("Network Addresses", "Broadcast Addresses")) 
    # Now, I try to print FinalReport in 2 columns. 
    for list in FinalReport: 
     for num in list: 
      print(formatted.format(num, num)) 
      break 

, 나는 최고로 문헌을 검토 한 나는 바로 인접한 열 한 열에서 하나 개의 함수의 출력 및 제 기능을 인쇄하는 방법을 가르치는 모든 문서를 발견하지 않았습니다 . 나는 틀릴 수 있었다. 이 훌륭한 공동체가 제공 할 수있는 도움에 감사드립니다. 고맙습니다.

답변

0

나는 당신이 해결 될 것 뭘 하려는지 생각 다음

['192.168.1.32', '192.168.1.31']

으로 : 당신이 전에했다

for list in FinalReport: print(formatted.format(list[0], list[1]))

각각의 반복 형태가 될 것입니다 내부 for 루프의 첫 번째 요소를 반복하고 192.168.1.32을 호출 한 다음 break을 호출하면 결코 루프가 실행되지 않습니다. 두 번째 요소.

제공되는 코드 스 니펫은 목록의 목록을 반복하고 각 쌍을 상대 인덱스로 액세스합니다.