472,958 Members | 1,964 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 1090

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
7
by: wmkew | last post by:
Hello everyone I'm encountering a R6002 Runtime error and several bugs when trying to generate a simple Managed C++ application with .NET 2003. The main problem seems to arise from linking with...
2
by: mike_li | last post by:
On Window 2000 Professional Server DB2 UDB Level: DB2 code release "SQL07029" with level identifie "030A0105" and informational tokens "DB2 v7.1.0.98", "n040510" and "WR21337". In the...
0
by: mike_li | last post by:
On Window 2000 Professional Server DB2 UDB Level: DB2 code release "SQL07029" with level identifie "030A0105" and informational tokens "DB2 v7.1.0.98", "n040510" and "WR21337". In the...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.