By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,767 Members | 1,334 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,767 IT Pros & Developers. It's quick & easy.

Overiding error message when using a python program

P: n/a
I've completely rewritten a calculator I wrote to help me learn Python.
After someone told me about the def command I reliesed that I could
make the program much better, but there is a very anoying problem which
ocours when I run the program.

Here is the code
<code>
# IMPOSRTS #
import sys
import os

# DEF'S #
def print_intro():
os.system('clear')
print "Welcome to Calc v 0.1a"
print "----------------------"

def main():
print_intro()
while True:
prompt_user()

def prompt_user():
userinput = input(">")

def fib(n): # write Fibonacci series up to n
"""Print a Fibonacci series up to n."""
a, b = 0, 1
while b < n:
print b,
a, b = b, a+b
print

def quit():
sys.exit()

# PROGRAM FLOW
main()
</code>

Now when I run this program and I type in a command which I have no
code for e.g. "pi" (which is 3,14....) I get the error message
"NameError: name 'pi' is not defined" and then the program quits.

I'm creating this program for my own use but naturally sometimes I
would make spelling mistakes (being a dyslexic and all) and so having a
long error message and having the program quit is more then a bit
irritating. It would be much more preferable if the program simply
wrote "Command not recognised" and then kept going. Is this possible?

Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 22 Apr 2005 07:45:40 -0700, al*****************@gmail.com > I'm
creating this program for my own use but naturally sometimes I
would make spelling mistakes (being a dyslexic and all) and so having a
long error message and having the program quit is more then a bit
irritating. It would be much more preferable if the program simply
wrote "Command not recognised" and then kept going. Is this possible?


It certainly is. "NameError" is what's called an exception, and you
can catch those. Try something like (untested):

try:
print 10 / pi
except NameError:
print "Command not recognised"

I can't see where you'd want that in your code, 'cos I can't see where
you are actually processing the user's input.

For more on exceptions, see <http://docs.python.org/tut/node10.html>.

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Jul 19 '05 #2

P: n/a
Thanks, this works great.

I edited def prompt_user() so that it now reads

<code>
def prompt_user():
try:
userinput = input(">")
except NameError:
print "Command not recognised"
</code>

Jul 19 '05 #3

P: n/a
al*****************@gmail.com said unto the world upon 2005-04-22 10:45:
I've completely rewritten a calculator I wrote to help me learn Python.
After someone told me about the def command I reliesed that I could
make the program much better, but there is a very anoying problem which
ocours when I run the program.

Here is the code
<code>
# IMPOSRTS #
import sys
import os

# DEF'S #
def print_intro():
os.system('clear')
print "Welcome to Calc v 0.1a"
print "----------------------"

def main():
print_intro()
while True:
prompt_user()

def prompt_user():
userinput = input(">")

def fib(n): # write Fibonacci series up to n
"""Print a Fibonacci series up to n."""
a, b = 0, 1
while b < n:
print b,
a, b = b, a+b
print

def quit():
sys.exit()

# PROGRAM FLOW
main()
</code>

Now when I run this program and I type in a command which I have no
code for e.g. "pi" (which is 3,14....) I get the error message
"NameError: name 'pi' is not defined" and then the program quits.

I'm creating this program for my own use but naturally sometimes I
would make spelling mistakes (being a dyslexic and all) and so having a
long error message and having the program quit is more then a bit
irritating. It would be much more preferable if the program simply
wrote "Command not recognised" and then kept going. Is this possible?


Sure, its possible. How to do it from where you are is a bit more
dark; you've not included the part of your code which acts on the
user's input. (And your prompt_user function should use raw_input and
return the user input for processing by other functions. raw_input is
safer; input executes arbitrary code.)

I see just before sending, that you seemed happy with Simon Brunning's
suggestion. But, as I hate to waste the typing, here's another way
sketched:

def funct1():
print 'This is funct1'

def funct2():
print 'This is funct2'

funct_dict = {'1': funct1, '2': funct2}
# strings as keys because of raw_input
# functions are objects, and thus can be values in a dict

def prompt_user():
return raw_input('Well?')

def dispatch(request):
function = funct_dict.get(request, None)
if function:
function()
else:
print 'You entered %s, but there is no such command' %request
Put that in a script and run

dispatch(prompt_user())

and you should see the desired behaviour. I will leave integrating the
idea into the structure you have as yet to you.

HTH,

Brian vdB

Jul 19 '05 #4

P: n/a
Brian van den Broek <bv****@po-box.mcgill.ca> wrote:
you've not included the part of your code which acts on the
user's input.
I think you'll find the answer to the question of where the code
that acts on the user's input lies here:
(And your prompt_user function should use raw_input and
return the user input for processing by other functions. raw_input is
safer; input executes arbitrary code.)

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Jul 19 '05 #5

P: n/a
Sion Arrowsmith said unto the world upon 2005-04-22 13:00:
Brian van den Broek <bv****@po-box.mcgill.ca> wrote:
you've not included the part of your code which acts on the
user's input.

I think you'll find the answer to the question of where the code
that acts on the user's input lies here:

(And your prompt_user function should use raw_input and
return the user input for processing by other functions. raw_input is
safer; input executes arbitrary code.)


Quite so :-[ I think I must have typed the two parts which you
quote with different hands, because obviously my brain didn't manage
to see them both at once.

Thanks for pointing that out.

Best,

Brian vdB
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.