471,317 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

Error in following code

Im working with python2.2 on red hat linux.
The following program is supposed to print decreasing numbers from an
entered number till 1, each decrement being = 1 :

#! usr/bin/env/python

def f(n=int(raw_input("enter number: "))):
print 'n=',n
if n>1:
return n*f(n-1)
else:
print 'end'
return 1


Though it works fine on the python interpretor, i dont get the
required output when i write this code in gedit (text editor). The
output that i get is (where t4.py is the name of the file):

[root@localhost root]# python t4.py
enter number: 6
[root@localhost root]#

i.e it takes the input but doesn't print anything. If anybody can
help... Thanx!

Jun 22 '07 #1
4 1038

za*****@gmail.com wrote:
[root@localhost root]# python t4.py
enter number: 6
[root@localhost root]#

i.e it takes the input but doesn't print anything. If anybody can
help... Thanx!
You're creating a function called "f" in the first line, but the script never does anything with it, so it never gets executed. You need to actually call your function after creating it:

-----

#! usr/bin/env/python

def f(n=int(raw_input("enter number: "))):
print 'n=',n
if n>1:
return n*f(n-1)
else:
print 'end'
return 1

f()

-----

That should do the trick.
Jun 22 '07 #2
za*****@gmail.com wrote:
Im working with python2.2 on red hat linux.
The following program is supposed to print decreasing numbers from an
entered number till 1, each decrement being = 1 :

#! usr/bin/env/python

def f(n=int(raw_input("enter number: "))):
print 'n=',n
if n>1:
return n*f(n-1)
else:
print 'end'
return 1


Though it works fine on the python interpretor, i dont get the
required output when i write this code in gedit (text editor). The
output that i get is (where t4.py is the name of the file):

[root@localhost root]# python t4.py
enter number: 6
[root@localhost root]#

i.e it takes the input but doesn't print anything. If anybody can
help... Thanx!
Hello,

When you run it through the interpreter, then the interpreter "looks"
at your definition of f, "understands" it, and continues on. What
follows your definition of f? Nothing. In particular, nothing instructs
the interpreter to *execute* f. So your problem is not that f is being
executed but is not printing anything, but rather that f is not being
executed.
To do what you want it to do, maybe try the following:
#! usr/bin/env/python

def f(n):
print 'n=',n
if n>1:
return n*f(n-1)
else:
print 'end'
return 1
if __name__ == '__main__':
n = int(raw_input("enter number: "))

f(n)
The line (if __name__...) means that if the interpreter is running your
module the way you mean here, then it should get the raw input for n,
then call f.

HTH,

Efrat

P.S. Note that I changed your f so that it doesn't do input itself.
Coupling calculation code with user-interaction code is maybe not so
good (IMHO).
Jun 22 '07 #3
za*****@gmail.com wrote:
Im working with python2.2 on red hat linux.
The following program is supposed to print decreasing numbers from an
entered number till 1, each decrement being = 1 :

#! usr/bin/env/python

def f(n=int(raw_input("enter number: "))):
print 'n=',n
if n>1:
return n*f(n-1)
else:
print 'end'
return 1
Though it works fine on the python interpretor, i dont get the
required output when i write this code in gedit (text editor). The
output that i get is (where t4.py is the name of the file):

[root@localhost root]# python t4.py
enter number: 6
[root@localhost root]#

i.e it takes the input but doesn't print anything. If anybody can
help... Thanx!
Don't do such experiments as root. It's too easy to totally mess up your
machine.

Peter
Jun 22 '07 #4
Jay Loden wrote:
That should do the trick.
Additionally, it does the trick to save the first entered number as
default argument forever.

Regards,
Björn

--
BOFH excuse #117:

the printer thinks its a router.

Jun 23 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Peter Frost | last post: by
6 posts views Thread by Peter Frost | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.