-4

I have actually made a program that will scan the whole Devices on the same network but the stupid issue that I got from python it turns out it was coming from python 3.10 not python 2.7 I can fix it by simply put raw_input("IP > ") But I'm Actually looking for The solution that will work with Python 3.10 so here is the actual code:

#!/usr/bin/env python
target_ip = input("IP > ") 
print(target_ip)

once I typed my Local IP:

`IP > 192.168.0.1`

it Gives me an Error Saying:

Traceback (most recent call last):
File "test.py", line 4, in <module>
target_ip = input("IP > ")
File "<string>", line 1
192.168.0.1
        ^
SyntaxError: invalid syntax

So Any Solution For This Guys I will Thankful.

  • 1
    You already know the solution: Use `raw_input` in Python 2, or `input` in Python 3. – khelwood May 14 '22 at 22:32
  • I'm confused. You want to make this work for Python 3.x but you also tagged it with Python 2.7. You say it works with `raw_input` so does it mean you are using Python 2.7 to run your program? Which version of Python do you want to target? – Gino Mempin May 14 '22 at 22:34
  • Seems like you just need to change your shebang from `#!/usr/bin/env python` to `#!/usr/bin/env python3` so you actually run with Python 3, and `input` works without trying to `eval` the input. – ShadowRanger May 17 '22 at 02:04
  • Nope I'm Just targeting Python 2.7 for more help if someones know the solution notice that my programs works fine with python 2.7 basically I want to make this program flexible with Python 3 because as you Guys know python 3 has some problem with data type unlike python 2 so given that I'm looking for solution . – Daniel Weaver May 17 '22 at 02:07
  • 1
    @DanielWeaver: "python 3 has some problem with data type" What on Earth do you mean by that? There are *porting* issues related to inconsistencies in the type model between 2 and 3, but if you're writing for plain Python 3, I have no idea what problems you're referring to. Python 2 left support two years ago; you should not be writing *new* code targeting Python 2. – ShadowRanger May 17 '22 at 12:23
  • 1
    @DanielWeaver: The solution is what khelwood said a week ago; on Python 2, you use `raw_input` to get the same result as `input` does on Python 3 (receiving a raw string without trying to `eval` it). You can do silly things like `try: input = raw_input` `except NameError: pass` to replace `input` with `raw_input` when you're on Python 2 and then use `input` unconditionally I guess, but the real answer is to stop writing new code in a dead language. – ShadowRanger May 23 '22 at 10:27
  • Thanks @ShadowRanger I got it after doing some research out the internet and stackoverflow one think to take into consideration is that in python 2 the only way to work with input function is to give it a string input e.g ==> `input("How old are you > ")` you should give it `> '5'` Using single quotes however I'm not quite sure that you understand me very well dude because what I meant is wanna a way to work with input in python2 and as you know input is removed from python2 and same goes for raw_input it also removed from python3 – Daniel Weaver May 24 '22 at 03:04
  • 1
    @DanielWeaver: `input` exists in Python 3. It's *exactly* equivalent to `raw_input` in Python 2. We've been telling you that over and over. If you used `raw_input` for Python 2 and `input` for Python 3, you wouldn't have to quote your inputs. But you keep ignoring us, thinking there is some solution other than "Use the correct API for the version of the language you're running on". – ShadowRanger May 24 '22 at 12:14

0 Answers0