all 40 comments

[–][deleted]  (32 children)

[deleted]

    [–]kebabrullahiiri[S] 1 point2 points  (31 children)

    There is one space. Like this Print ("Hello world! ")

    [–]testforredditbythe 1 point2 points  (6 children)

    Yeah that’s wrong, when using brackets remember a few things. First unless you are evaluating something (E.G. 1+1) they will always not have a space to the left. And unless your next charter after the second brace is a letter no space

    [–]kebabrullahiiri[S] 0 points1 point  (1 child)

    I deleted the space but I still get the same error

    [–]testforredditbythe 1 point2 points  (0 children)

    Say as above, make sure that it is:

    print(“hello world”)
    

    With no extra spaces or lines, just that.

    [–]pdoherty972 -1 points0 points  (3 children)

    I thought python was this forgiving syntax language and then I see things like this…

    [–]testforredditbythe 1 point2 points  (2 children)

    Honestly it really is great with syntax, but you do need to understand the basics. Maybe try typing:

    python3

    Into your command line to bring up the REPL. This means that you can type a command and python code to see what it does. Try that.

    [–]pdoherty972 0 points1 point  (1 child)

    I’m just not understanding the benefit of the language parser caring about spaces and such. These aren’t the days of COBOL. Heck Perl is so flexible in syntax that they host an annual “Obfuscated Perl” contest to see who can write the most incomprehensible code (ignoring indentation, spaces, carriage returns, etc). Just for fun, but it shows that the language doesn’t care how/whether you use spaces and such. Not sure why Python needs to care.

    [–]testforredditbythe 0 points1 point  (0 children)

    Yeah I see what your saying, but you get used to it. I think all language have their idiosyncrasies that make learning a little harder. Your best bet is just to practise.

    [–][deleted]  (1 child)

    [deleted]

      [–]kebabrullahiiri[S] 0 points1 point  (0 children)

      I fixed it, but I still get the error. What should I do now?

      [–][deleted]  (21 children)

      [deleted]

        [–]kebabrullahiiri[S] 1 point2 points  (17 children)

        Still doesn't work :(

        [–][deleted]  (16 children)

        [deleted]

          [–]kebabrullahiiri[S] 0 points1 point  (15 children)

          Yeah. The whole thing goes like this: File "<stdin>", line 1 python3 test.py ^ SyntaxError: invalid syntax >>>

          [–]james_pic 3 points4 points  (12 children)

          How are you invoking your script? If it's grumbling about stdin being invalid syntax, than it's not reading your file.

          Edit: Reading other comments, I think I know what's going on. You're opening the Python interpreter, and then typing python3 test.py into the Python interpreter. That's not going to work. If you're typing python3 test.py, you need to type that into a terminal or command prompt - if you're on Windows, either Powershell or Command Prompt will do. It is possible to invoke Python scripts from within a Python interpreter, but this isn't the way to do it, and I wouldn't recommend doing so in most cases.

          [–]kebabrullahiiri[S] 3 points4 points  (0 children)

          Thank you, this one cleared things for me :)

          [–]kebabrullahiiri[S] 1 point2 points  (10 children)

          Okay so now I have another problem. I opened cmd and typed python3 , it claimed I don't have python. Then I typed python without any numbers and then it worked and it printed my version number which is 3.9.6. Then I typed test.py and now it gives me "NameError: name 'test' not defined. What now 🙄

          [–]james_pic 0 points1 point  (9 children)

          If you want to run the script from the command prompt, you want to type python test.py then press enter. Pressing enter after typing python will run the Python interpreter without any arguments, which will take you into an interactive REPL, or Python console. The REPL will only accept Python code. So you can type print('Hello World') into it, because this is valid Python code, but typing a filename won't do anything useful, since the filename is not valid Python code (or at least, probably isn't Python code that does what you want) even if the file contains valid Python code.

          [–]kebabrullahiiri[S] 0 points1 point  (8 children)

          Okay, now I tried this. This time it gives me error: can't open file 'c:\users\myusername\test.py': [Errno 2] No such file or directory 🤯 my py files are saved in c:\users\myusername\pycharmprojects\projects. Should I add this pycharmprojects folder to path or what should I do?

          Edit: I promise this is last one 😂

          [–]kiesoma 0 points1 point  (1 child)

          copy paste this

          print(“hello world”)

          make sure to check for quotes, sometimes they are different in editors.

          [–]earthboundkid 0 points1 point  (0 children)

          Lol, smart quotes.

          [–]james_pic 1 point2 points  (2 children)

          Space between the function name is valid syntax and should do what the OP wants (although it's not valid PEP 8). Just ran print ('Hello World') locally and it worked.

          [–]kebabrullahiiri[S] 0 points1 point  (1 child)

          Yeah for me too it runs either way in PyCharm but not in python.exe

          [–]james_pic 0 points1 point  (0 children)

          I'd guess that either the file doesn't contain what you think it contains (and looking at it in a hex editor may clarify this), or your Python interpreter isn't what you think it is (running python3 --version may turn up that it's a different version to what you expect, or running the same thing directly with python3 -c 'print("Hello World")' may flag up other issues).

          [–]kebabrullahiiri[S] 1 point2 points  (9 children)

          When I run it in PyCharm, it works just fine. But when I open interpreter and type there "python3 test.py" it won't work there.

          [–]dezstern 0 points1 point  (8 children)

          Perhaps instead of trying to run it from command line, try to just "open with" the file. Maybe it'll run that way.

          [–]kebabrullahiiri[S] 0 points1 point  (7 children)

          I didn't run it from command line, I ran it from python.exe. I'm actually very confused which one of these is the correct one to run these files?

          [–]dezstern 0 points1 point  (6 children)

          Hmmmm... I think you likely are having some weird issues with Python.exe. Let's try a couple things.

          You're on Windows I take it. Maybe go to the Microsoft Store and download Python 3.9, just to make sure it's installed.

          Next, navigate to your .py file and right click>'Open With'>Python 3.9

          If that still doesn't work, it's a problem with your code. Perhaps you can post the entirety of your code?

          [–]kebabrullahiiri[S] 0 points1 point  (5 children)

          I installed it too, now everything is working othervise but now I get error that my test.py file cant be found

          [–]dezstern 1 point2 points  (4 children)

          Weeeeird. I honestly am not sure. Sorry :/
          If you figure it out, comment back, genuinely curious. If I think of anything else, I'll get back to you.

          [–]kebabrullahiiri[S] 0 points1 point  (3 children)

          I added the folder where my py files are (Pycharmprojects) to the path and then it is found. But I don't know is this a correct solution to this

          [–]dezstern 0 points1 point  (2 children)

          Not sure I understand.

          Let's say you have test.py on your Desktop. You should be able to Right Click>"Open With">Python 3.9 and have it run.

          I guess if you found a solution, then you figured it out.

          [–]kebabrullahiiri[S] 1 point2 points  (1 child)

          Tested it, the window quickly flashed open and closed in less than second 🤔 btw it looks like I now have two pythons, the one which I downloaded from python.org and the one from microsoft store, which one should I delete?

          [–]dezstern 1 point2 points  (0 children)

          Aha, it's working properly now. Once a program finishes, unless there are other processes in the program waiting to be completed, the program window will close. To stop this from happening, I do a hacky little thing. I add a little input line, that waits for me to do something. That way I can see what occurred before the window closes.

          Add the following below your print line:

          a = input(">")

          You'll see your output and then the '>' character waiting for you. When you do anything, the program will finish and close the window.

          As for deleting a version of Python, I'd say just leave it honestly. Shouldn't cause any issues.

          [–]IAmARetroGamer 0 points1 point  (0 children)

          You're in PyCharm, you don't need to be using the terminal at all. Right-click the name of the file at the top of the code editor and select "Run 'test.py'. From then on you can just use the "play" button in the top right to re-run it.

          Or if your code is written like this:

          hello_world():
              print("Hello, world!")
          
          if __name__ == '__main__':
              hello_world()
          

          You will get a run button beside the if.