0

This script takes whatever is inputted and formats it into a "1/1" type format.

It works with the first two elif statements, but when moving onto the third it still assigns the 2/ infront of the numbers. It's supposed to jump to 3/ and 4/ as you see below.

Your help is greatly appreciated.

import re

port = input("Enter port number:")

if bool(re.search('\/', port)) == True:
    p = port

elif int(port) <= 48:
    port = "1/" + str(port)

elif int(port) >= 53 <= 100:
    port = "2/" + str(port)

elif int(port) >= 105 <= 152:
    port = "3/" + str(port)

elif int(port) >= 157 <= 204:
    port = "4/" + str(port)

print(port)
Ralf
  • 16,086
  • 4
  • 44
  • 68

3 Answers3

4

The problem is where you're trying to chain comparisons:

elif int(port) >= 53 <= 100:

This checks to see if int(port) >= 53 and 53 <= 100. Since 53 <= 100 is always true, this block will catch anything where int(port) >= 53. What I assume you mean to do is:

elif 53 <= int(port) <= 100:

This will only catch the cases where int(port) is between 53 and 100 (inclusive). You'll need to make similar changes to the rest of the elif blocks.

glibdud
  • 7,550
  • 4
  • 27
  • 37
1

You got your if-conditionals mixed up. Fix:

def getPort(port):

    # https://stackoverflow.com/questions/12265451/ask-forgiveness-not-permission-explain
    # only convert to int once, if that does not work check for / else bail out
    try:
        p = int(port)
    except ValueError:
        if "/" in port: # no need for regex
            return port
        else:
            raise ValueError("Port either integer or somthing with / in it")

    if p <= 48:               # 49-52 are not covered
        port = "1/" + port

    elif  53 <= p <= 100:     # move the condition between, the way you did it theyre True
        port = "2/" + port    # no matter because 53 <= 100 all the time

    elif 105 <= p <= 152:     # 101-104 are not covered
        port = "3/" + port

    elif 157 <= p <= 204:     # 152-156 are not covered
        port = "4/" + str(port)

    else:
        raise ValueError("Port either integer or somthing with / in it")

    return port

for p in ["1","54","99","121","180","47/11","2000"]:
    try:
        print(getPort(p))
    except ValueError as e:
        print (e)

Output:

# Input: ["1","54","99","121","180","47/11","2000"]

1/1
2/54
2/99
3/121
4/180
47/11
Port either integer or somthing with / in it

You got some missing port ranges, 50 f.e. is not covered and would result in ValueError.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
1

Your error is here:

elif int(port) >= 53 <= 100:

This translates to (you can read about chained comparisson operators):

elif int(port) >= 53 and 53 <= 100:

and will allways be True because of the second part; so thats why the later elif never get reached.


My suggestion:

port = input("Enter port number:")
int_port = int(port)    # so we don't repeat the same operation multiple unnecessary times

if bool(re.search('\/', port)):
    pass
elif int_port <= 48:
    port = "1/" + port
elif 53 <= int_port <= 100:
    port = "2/" + port
elif 105 <= int_port <= 152:
    port = "3/" + port
elif 157 <= int_port <= 204:
    port = "4/" + port

print(port)
Ralf
  • 16,086
  • 4
  • 44
  • 68