Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 A super simple python problem (I'm assuming).
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
canmovie




PostPosted: 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: (Smile [], 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?
Sponsor
Sponsor
Sponsor
sponsor
Dreadnought




PostPosted: 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:
code:
python -i a5.py


EDIT: Alternatively I think you can also run your script from within the interpreter like so (assuming your using python 2.X)
code:
PS C:blah> python
Python <lots of information...>
Type "help", <blah blah blah...>
>>> execfile("a5.py")
>>>
btiffin




PostPosted: 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/
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: