Computer Science Canada A super simple python problem (I'm assuming). |
Author: | canmovie [ Tue Sep 17, 2013 12:48 pm ] |
Post subject: | A super simple python problem (I'm assuming). |
I just started learning python and coding functions and up to yesterday, everything was going fine, but now whenever I try to run any function from PowerShell I get an error message. NO FUNCTIONS WILL WORK FOR SOME REASON E.G This is the "a5.py" file I wrote in Notepad++: ----------------------------------------------------- def random_func(): print "Hello." random_func() ----------------------------------------------------- and this is what I got in PowerShell: ----------------------------------------------------- PS C:\Users\samsung\Documents> python a5.py Hello ----------------------------------------------------- Perfect so far.... but when I comment out the function call (random_func()) in the "a5.py" file and try to call the function using PowerShell, this happens: ----------------------------------------------------- PS C:\Users\samsung\Documents> python a5.py PS C:\Users\samsung\Documents> random_func() At line:1 char:13 + random_func() + ~ An expression was expected after '('. + CategoryInfo : ParserError: ( [], ParentContainsErrorRecordException + FullyQualifiedErrorId : ExpectedExpression ----------------------------------------------------- ALL I DID WAS TAKE OUT THE FUNCTION CALL IN THE NOTEPAD FILE. WHY CAN'T I EXECUTE THE FUNCTION FROM POWERSHELL??? Sorry but this is super frustrating I can't execute a single function, any idea what the problem may be? |
Author: | Dreadnought [ Tue Sep 17, 2013 1:15 pm ] | ||||
Post subject: | Re: A super simple python problem (I'm assuming). | ||||
You can't call functions from your file outside the python interpreter like you are trying to do. You ran your script, it exited and then you tried calling one of its functions, which failed ) since your script exited and powershell didn't recognize the syntax. To run your script in the python interpreter and have a prompt afterwards use the -i flag. Example:
EDIT: Alternatively I think you can also run your script from within the interpreter like so (assuming your using python 2.X)
|
Author: | btiffin [ Tue Sep 24, 2013 12:05 am ] |
Post subject: | RE:A super simple python problem (I\'m assuming). |
Dreadnought; Just in case you've never seen. Look at IPython. Well done, interactive Python (for scientists). Web Notebook interface as well, shared Python code running in a browser view, pretty cool. With charts and graphs and plots and, well, nerd stuff. 1.0 reference recently released. http://ipython.org/ |