Skip to main content
 首页 » 编程设计

python之将 for 循环中的 IP 地址字符串附加到列表中

2024年10月24日27unruledboy

我正在尝试创建一个脚本,该脚本将获取主机名和 IP 地址并将它们写入一个列表,我可以将该列表压缩到一个单独的字典中。它的工作除了我在变量中没有任何数字我只是得到“。”这一事实之外。部分而不是数量。不太确定我做错了什么,因为我以前没有发生过...看看:

HOST_NAME = [] 
IP_ADDRESS = [] 
 
ADDITION_NAME = "Please enter a word or two explaining the addition (used for file name): " 
 
ENTRY_AMOUNT = int(input("How many hosts will need records? ")) 
 
for number in range(ENTRY_AMOUNT): 
    hostname = raw_input("What is the hostname of the host: ") 
    address = raw_input("What is the IP address of the host: ") 
    HOST_NAME.append(hostname) 
    IP_ADDRESS.append(IP_ADDRESS) 
 
 
A_RECORD_ENTRY = dict(zip(HOST_NAME,IP_ADDRESS)) 
 
 
print HOST_NAME # test for correct appendages 
print IP_ADDRESS # test for correct appendages 
print A_RECORD_ENTRY # testing code for dictionary output 

这给我的输出是:

C:\Users\fallacy>a_record_add.py 
How many hosts will need records? 1 
What is the hostname of the host: test 
What is the IP address of the host: 192.168.1.1 
['test'] 
[[...]] 
{'test': [[...]]} 

如前所述,它只添加了点我以前没有遇到过这个所以请让我知道我做错了什么!非常感谢!

请您参考如下方法:

IP_ADDRESS.append(IP_ADDRESS) 

我想你的意思是:

IP_ADDRESS.append(address)