1 回答
TA贡献1906条经验 获得超10个赞
为什么需要循环?使用in检查,如果在列表中存在的输入值,并相应如下:
option=input("option: ")
iplist=['192.168.1.1', '192.168.1.2', '192.168.1.254']
if option in iplist:
# do the rest
pass
或:
如果要获取Index列表中元素的 :
for index, elem in enumerate(iplist):
if option == elem:
print("Element found at Index: {}".format(index))
输出:
option: 192.168.1.2
Element found at Index: 1
编辑 2:
首先要注意以下几点:
从用户那里获取输入并将其转换为 anint因为您无法使用 str 索引访问列表:
我仍然没有看到循环点
所以:
import subprocess
option= int(input("option: ")) # 1
iplist=['192.168.1.1', '192.168.1.2', '192.168.1.254']
nub = iplist[option]
subprocess.call(["ping", nub])
输出:
Pinging 192.168.1.2 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Ping statistics for 192.168.1.2:
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
添加回答
举报