raw_input没有要求用户输入

时间:2017-11-22 14:08:46

标签: python python-2.7 while-loop raw-input

当我运行此代码时,它不会要求用户输入,而只是显示黑屏不到一秒钟。我也没有错误。可能是什么问题?

#Check an IP address validity
while True:
    ip_address = raw_input("Enter an IP address: ")
    a = ip_address.split(".")
    if ((len(a) == 4) and (a[0] =< 223) and (a[0] != 127) and (a[0] != 169 
or a[1] != 224) and (0 =< a[0] =< 255 and 0 =< a[1] =< 255 and 0 =< a[2] =< 
255 and 0 =< a[3] =< 255)):
        break

    else:
        print("The IP address is not valid")

1 个答案:

答案 0 :(得分:1)

有多个<=错误,此代码正常运行。

while True:
     ip_address = raw_input("Enter an IP address: ")
     a = ip_address.split(".")
     if ((len(a) == 4) and (a[0] <= 223) and (a[0] != 127) and (a[0] != 169 or a[1] != 224) and (0 <= a[0] <= 255 and 0 <= a[1] <= 255 and 0 <= a[2] <= 255 and 0 <= a[3] <= 255)):
         break

    else:
         print("The IP address is not valid")

因为如果您不输入IP地址并运行脚本,可能会遇到while True:此错误

Enter an IP address: Traceback (most recent call last):
  File "main.py", line 3, in <module>
  ip_address = raw_input("Enter an IP address: ")
  EOFError: EOF when reading a line