-1

When I run this code, it is not asking the user for an input but just showing a black screen for less than a second. I don't get errors as well. What could be the problem?

#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")
TheQuestioner
  • 724
  • 2
  • 8
  • 13
Steve
  • 81
  • 1
  • 7
  • 1
    Python 2 or Python 3? `raw_input()` is in Python 2 only. – Elis Byberi Nov 22 '17 at 14:09
  • 2
    How do you run the code? Is this the **whole** code? Aren't you able to edit your question? @ElisByberi: you might have answered your own question. – CristiFati Nov 22 '17 at 14:11
  • @SteveRachidi Again, it is is Python 2 or Python 3. However, your code does raise `SyntaxError` exception. – Elis Byberi Nov 22 '17 at 14:12
  • @CristiFati There is not need to run code. `print()` is default in Python 3 but works in Python 2 too. `raw_input()` is in Python 2 only. – Elis Byberi Nov 22 '17 at 14:14
  • This is python 2, and yes this is my whole code – Steve Nov 22 '17 at 14:14
  • Not sure how you're not getting errors... that code is riddled with syntax errors (hint: `=<` is not a valid python operator). – glibdud Nov 22 '17 at 14:17
  • *lower than or equal* is `<=`, not `=<`. – keepAlive Nov 22 '17 at 14:17
  • `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)):` When youre comparing the the parts of the ip with <= youre compairing it as a string as raw_input will store it as a string. try casting it as an int using `int(a[0])` before comparing it – wtreston Nov 22 '17 at 14:17
  • 1
    @SteveRachidi Your if statement has too many `SyntaxError` errors. Fix your code. – Elis Byberi Nov 22 '17 at 14:17
  • @ElisByberi: you did answer your own question (was obvious). And it **is** required to run apiece of code to get some output (whatever that would be) out of it. Apparently OP doesn't run it (or this is what their "output" suggests). SteveRachidi: please let us know how do you run (or you think you run) **this** code (that you posted). – CristiFati Nov 22 '17 at 14:25
  • @CristiFati What if OP was using `raw_input()` in Python 3? I would not be asking if it was not the case. It may be the only problem. You can not be sure. – Elis Byberi Nov 22 '17 at 14:33
  • @SteveRachidi Please provide a [Minimal, Complete, and Verifiable example!](https://stackoverflow.com/help/mcve) – Elis Byberi Nov 22 '17 at 14:40

1 Answers1

1

There are multiple <= errors and this code is working.

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")

because it is in while True: this error you may encounter if you will not enter IP address and run the script

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
Smit Shilu
  • 345
  • 3
  • 17
  • I thought that even if I have errors in the if statement, the code should stop on the raw_input statement waiting for the user input. Thank you all – Steve Nov 22 '17 at 15:54
  • python compiles first before running and in that compilation it checks for syntax errors, so in your case it was not reaching at running state because of syntax error. – Smit Shilu Nov 22 '17 at 16:24