Hi folks Im taking a free self paced web class .. the following code meets the exercise requirement.
I want to loop through the list, look at each octet once .. convert to a bin value and hex and assign to respective vars to be printed later in the script. I added a for loop shown below. The code is not working.
My question is .. How do I setup the loop to create vars
boct1 - var containing the binary equivalent of octet one
hoct1 - var containing the hex equivalent of octet one
boct2 - var containing the binary equivalent of octet two
hoct2 - var containing the hex equivalent of octet two
etc etc
My attempt at the loop is as follows :
Sum
... geez .. soon as I send these .. I get it ..
just append each to a binarylist and hexlist respectively .. duhhh !!
ip = input("Enter an ip address : ") octets = ip.split(".") print("\n\n{:^15}{:^15}{:^15}{:^15}".format("Octet1","Octet2","Octet3","Octet4")) print("-" * 80) print("{:^15}{:^15}{:^15}{:^15}".format(*octets)) boct1 = bin(int(octets[0])) boct2 = bin(int(octets[1])) boct3 = bin(int(octets[2])) boct4 = bin(int(octets[3])) print("{:^15}{:^15}{:^15}{:^15}".format(boct1,boct2,boct3,boct4)) xoct1 = hex(int(octets[0])) xoct2 = hex(int(octets[1])) xoct3 = hex(int(octets[2])) xoct4 = hex(int(octets[3])) print("{:^15}{:^15}{:^15}{:^15}".format(xoct1,xoct2,xoct3,xoct4)) print("-" * 80)I dont like whats going on on lines 6-9 and 11-14. Seems cheap to me.
I want to loop through the list, look at each octet once .. convert to a bin value and hex and assign to respective vars to be printed later in the script. I added a for loop shown below. The code is not working.
My question is .. How do I setup the loop to create vars
boct1 - var containing the binary equivalent of octet one
hoct1 - var containing the hex equivalent of octet one
boct2 - var containing the binary equivalent of octet two
hoct2 - var containing the hex equivalent of octet two
etc etc
My attempt at the loop is as follows :
ip = input("Enter an ip address : ") octets = ip.split(".") # for i in range(len(octets)): print(octets[i]) boct[i] = bin(int(octets[i])) xoct[i] = hex(int(octets[i])) # print("\n\n{:^15}{:^15}{:^15}{:^15}".format("Octet1","Octet2","Octet3","Octet4")) print("-" * 80) print("{:^15}{:^15}{:^15}{:^15}".format(*octets)) boct1 = bin(int(octets[0])) boct2 = bin(int(octets[1])) boct3 = bin(int(octets[2])) boct4 = bin(int(octets[3])) print("{:^15}{:^15}{:^15}{:^15}".format(boct1,boct2,boct3,boct4)) xoct1 = hex(int(octets[0])) xoct2 = hex(int(octets[1])) xoct3 = hex(int(octets[2])) xoct4 = hex(int(octets[3])) print("{:^15}{:^15}{:^15}{:^15}".format(xoct1,xoct2,xoct3,xoct4)) print("-" * 80)Thanks folks
Sum
... geez .. soon as I send these .. I get it ..
just append each to a binarylist and hexlist respectively .. duhhh !!