-1

I am new to Python and programming as a whole and I apologize for asking what may seem to be a duplicate question. However, I have been unable to run my own code using the following format:

C:\Users\Archangel>python hello.py

This is what hello.py contains:

# Define a function
def world():
    print("Hello, World!")

I get the following response:

C:\Users\Archangel>python hello.py
  File "hello.py", line 1
    Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
             ^
SyntaxError: invalid syntax

I have made sure the python file is in the C:\Users\Archangel folder. I should mention that I have tried having and still have the file (and other files that will not run as well) in locations contained in Path such as C:\Users\Archangel\AppData\Local\Programs\Python\Python37-32

The following is what I get from print(sys.path)

>>> import sys
>>> print(sys.path)
['', 'C:\\Users\\Archangel\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.zip', 'C:\\Users\\Archangel\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs', 'C:\\Users\\Archangel\\AppData\\Local\\Programs\\Python\\Python37-32\\lib', 'C:\\Users\\Archangel\\AppData\\Local\\Programs\\Python\\Python37-32', 'C:\\Users\\Archangel\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages']
>>>

My Google searches have been fruitless and hours of going through similar questions here on Stack Overflow have not helped. Can anyone help or point me to a question that has been answered?

UPDATE

My programs contained the following lines: Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. from the IDLE shell I used to create the files.

Deleting these lines has solved the problem. Thanks to all of you. I think this question should be deleted since the issue is elementary and clearly an error on my part.

Twstr
  • 13
  • 4

2 Answers2

0

There is no error in function definition, even without function call it should just execute without any output.

The error appears because python clearly thinks that the first line in the file looks like this:

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32

You can open the file in an alternative editor (notepad++ for example) and make sure that the file doesn't contain this line.

After that to actually get the "Hello World" output you need to add function call:

def world():
    print("Hello, World!")

world()
Alex Nvm
  • 1
  • 1
-1

I can see below works fine.

# Define a function
def world():
    print("Hello, World!")

if __name__ == "__main__":
    world()
gsmaker
  • 533
  • 1
  • 4
  • 21
  • I do not see what in this is the actual answer. Are you saying that adding the `if __name__` part will solve the problem faced by OP? If so, could you elaborate? – FlyingTeller Aug 29 '18 at 12:24