-7

The less than operator is not working correctly for me:

port = raw_input("Enter Port: ")
        if port <= 65535:
            print("Valid Port")
        else:
            print("Port Invalid")

However, whenever I type in a port from 66 to 99 in ANY ten or hundred for example 166 or 766 or 889 it will be invalid, but anything below will be fine. It is like it is only registering/seeing the "65" part. I have tried without the "" around the 65535 but that always returns invalid.

Alright, got it sorted, I did not know that it returned as a string. Although, I am kind of disappointed that I was ripped apart because of this and I am not up to the "high brow" standards of all of the people with years of experience.

Bitten Fleax
  • 262
  • 4
  • 20

2 Answers2

7

You are comparing lexicographically. You should cast your input to a number, and compare it to a number as well:

port = int(raw_input("Enter Port: "))
if port <= 65535:
    print("Valid Port")
else:
    print("Port Invalid")

Consider also that, in Python 2, you can use the input() function to read integers instead of raw_input(). The behavior will be equivalent to eval(raw_input()) so it will return an integer if the user types an integer, a float if the user types a float, and so on.

However, keep in mind that Python 3 removes both functions and replaces them with a single input() function that works like Python 2's raw_input(). So, I suggest to use int(raw_input()) over input() because it will be easier to port to Python 3, and also because "Explicit is better than implicit".

William
  • 2,695
  • 1
  • 21
  • 33
1

Make sure to compare the integer, and not the string. e.g convert the string to integer before doing a comparision:

if int(port) <= 65535:
  print("Valid Port")
else:
  print("Port Invalid")
leonsas
  • 4,718
  • 6
  • 43
  • 70