473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1109

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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.