-2

Can someone explain why else is getting IndentationError: unexpected unindent while i try to work further on my if else loop.

Basicly am working on a port scanner project where i need to get some user input where out of this user input, i will specify how many ports there is needed to scan. My plan was to work further on the if command, so if i do not get the response of 1 the meaning was to next. But it do not seem like to work

Whats wrong?

input = input("1 or 2")

if input == '1':
        try:
                for port in range(1,65535):
                        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                        socket.setdefaulttimeout(1)

                        result = s.connect_ex((target,port))

                        if result ==0:

                                print("Port {} is open".format(port))

                        s.close()

else:
        print("do not work")

        except KeyboardInterrupt:
                print("\n ")
                sys.exit()
        except socket.gaierror:
                print("\n ")
                sys.exit()
        except socket.error:
                print("\")
                sys.exit()
n0zk
  • 13
  • 1
  • 5
  • 2
    The `except` clause(s) have to be in the same block of code (in this case, the body of the `if`) as the corresponding `try` statement. That `else` cannot separate them. – jasonharper May 12 '22 at 00:09
  • You don`t need else just put your try above if input == '1': and make type your code with the correct indent! Also note that you have no definition for target, where is it? – Mohamad Ghaith Alzin May 12 '22 at 00:29
  • Does this answer your question? [I'm getting an IndentationError. How do I fix it?](https://stackoverflow.com/questions/45621722/im-getting-an-indentationerror-how-do-i-fix-it) – Nick May 12 '22 at 00:52
  • 1
    Off-topic: I suggest that you read and start following the [PEP 8 - Style Guide for Python Code](https://www.python.org/dev/peps/pep-0008/) which recommends indenting Python coded with 4 space characters (not tab characters like you are doing). – martineau May 12 '22 at 00:53

2 Answers2

1

I’d have to say it’s that you’ve got an else in between your except statements, separating it from the try… right? Try something like this maybe

input = input("1 or 2")

if input == '1':
        try:
                for port in range(1,65535):
                        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                        socket.setdefaulttimeout(1)

                        result = s.connect_ex((target,port))

                        if result ==0:

                                print("Port {} is open".format(port))

                        s.close()

        except KeyboardInterrupt:
                print("\n ")
                sys.exit()
        except socket.gaierror:
                print("\n ")
                sys.exit()
        except socket.error:
                print("\n ")
                sys.exit()

else:
        print("doesn’t work")
Pwuurple
  • 352
  • 2
  • 11
0

You need to indent the else clause to be in line with the if result == 0 line. Once you do that, it should work. Your code should look like this:

userInput = input("1 or 2")

if userInput == '1':
        try:
                for port in range(1,65535):
                        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                        socket.setdefaulttimeout(1)

                        result = s.connect_ex((target,port))

                        if result ==0:

                                print("Port {} is open".format(port))
                        else:
                                print("do not work")
                        s.close()

        except KeyboardInterrupt:
                print("\n ")
                sys.exit()
        except socket.gaierror:
                print("\n ")
                sys.exit()
        except socket.error:
                print("\n")
                sys.exit()
2pichar
  • 1,348
  • 4
  • 17