嘿,我刚刚尝试把我的第一个项目放在一起,但我有for循环的问题,我很新的python。我的理解是这样的:Python for循环#的小数?
for a in range(1,10):
print(a)
我试图线沿线的做一些事情:
for ipscan in range(192.168.0.1,192.168.0.256):
print(ipscan)
嘿,我刚刚尝试把我的第一个项目放在一起,但我有for循环的问题,我很新的python。我的理解是这样的:Python for循环#的小数?
for a in range(1,10):
print(a)
我试图线沿线的做一些事情:
for ipscan in range(192.168.0.1,192.168.0.256):
print(ipscan)
for ipscan in range(1,256):
print('192.168.0.%s' % ipscan)
不要误会我的意思。我只是不想过度的事情。
@Thomas结束或范围是非包含的... – 2015-02-11 08:08:26
@JonClements谢谢,我现在不会那么愚蠢! – 2015-02-11 08:09:17
@Thomas哈哈我知道,python将排除范围内的最后一个数字,这就是为什么我把256,你仍然可以有一个*。*。*。255;) – lockdown 2015-02-11 08:09:54
范围是数字换货而已,所以在你的榜样,你需要在IP只为最后一个数字创建范围:
for ipscan in range(1,256):
print('192.168.0.%s' % ipscan)// this will create all wanted numbers
或者尝试使用字符串格式的方法:
format_str = '192.168.0.{0}'
for i in range(0,256):
print format_str.format(i)
它工作吗?你有错误吗? – 2015-02-11 08:05:54
没有即时得到“ParseError:第1行上的错误输入” – lockdown 2015-02-11 08:06:28
范围将无法直接用于IP地址,或者只有1到256之间的范围或使用处理IP地址的类 - [第一个谷歌结果为python IP地址范围](https://pythonhosted.org/netaddr/tutorial_01.html#support-for-non-standard-address-ranges) – Sayse 2015-02-11 08:06:52