现在,我解决它。它显示正确的输出。唯一的问题是它的输出有大括号和引号的每个字符串索引,因为它仍然是一个字符串。如何从输出中删除大括号?在python中有一个命令吗?
networkAddress = raw_input('Enter network address: ')
n = input('Enter number of network bits: ')
h = 32-n
hostAddressOut = 2**h
hostAddress = 0
x = input('Enter number of subnets: ')
octet = networkAddress.split('.')
for i in range(x):
if hostAddress > 255:
hostAddress = 0
thirdOctet = int(octet[2])+1
octet[2] = str(thirdOctet)
octet[3] = str(hostAddress)
print 'subnet', i, ' = ', octet
hostAddress = hostAddressOut
else:
octet[3] = str(hostAddress)
print 'subnet', i, ' = ', octet
hostAddress = hostAddress + hostAddressOut
这是示例输出。 enter image description here
它没有显示任何输出,因为你的最终if
是:
if '.' == 2:
这个条件总是返回false
,因此没有打印任何内容。
也在这里:
if i == '.':
if '.' == 2: # As mentioned this is incorrect
start = int(i)
# .....something
你正在检查i == '.'
然后做int(i)
。这将始终引发错误,因为您无法转换“。”到int
。