Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 22 Online
OpenStudy (anonymous):

Hey Open Study, I'm on chapter 8 of the course (OCW) and trying to run some of the scripts provided in the Lecture Handouts but have run into a problem. I'm using the gedit txt editor and running in the Command window. I've tried various combinations of $ python exp1.py or $ python $ import exp1 but when I try to run the function exp1(a, b) I can't get it to work. Any help gratefully received. It feels like a simple problem - I think I'm just missing something obvious!

OpenStudy (anonymous):

http://pastebin.com/u0eGxVgf

OpenStudy (anonymous):

Hi bwCA - thanks for the code - here's the exception I'm getting from the Terminal Shell when I try to run it there - $ python exp1.py $ exp1(1, 2) -bash: syntax error near unexpected token `1,' My code is (directly from the lecture handout): def exp1 (a,b): ans = 1 while (b>0): ans *= a b -= 1 return ans

OpenStudy (anonymous):

Try running it using IDLE. What you are doing is trying to use bash to interpret exp1(1,2). It does not know how to do it. Instead, add print exp(1,2) into your source code and execute it via bash invoking python exp1.py It should work, then.

OpenStudy (anonymous):

for it to work in your interpreter, try: $ python >>> import exp1 # assumes exp1.py in the default path >>> exp1.exp1(1,2) # the first exp1 is the import module, the second is the function

OpenStudy (anonymous):

Many thanks bmp & philips13 - both work a charm. I knew it was only one small step that I was missing!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!