一种程序,它询问网络地址、主机位数和子网数,然后显示正确的子网地址

2024-10-01 13:28:13 发布

您现在位置:Python中文网/ 问答频道 /正文

现在,我把它修好。它显示正确的输出。唯一的问题是,它的输出有大括号和引号的每个字符串索引,因为它仍然是一个字符串。如何从输出中删除大括号?在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


Tags: of字符串numberinputnetwork大括号printenter
1条回答
网友
1楼 · 发布于 2024-10-01 13:28:13

它不显示任何输出,因为您的最终if是:

if '.' == 2:

这个条件总是返回false,因此不打印任何内容

此外,这里:

if i == '.':
    if '.' == 2:  # As mentioned this is incorrect
        start = int(i)
        # .....something

您正在检查i == '.',然后执行int(i)。这将始终引发错误,因为无法将“.”转换为int

相关问题 更多 >