Overiding error message when using a python program

  • Thread starter aleksander.helgaker
  • Start date
A

aleksander.helgaker

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?
 
S

Simon Brunning

On 22 Apr 2005 07:45:40 -0700, (e-mail address removed) > 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>.
 
A

aleksander.helgaker

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>
 
B

Brian van den Broek

(e-mail address removed) 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
 
S

Sion Arrowsmith

Brian van den Broek said:
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:
 
B

Brian van den Broek

Sion Arrowsmith said unto the world upon 2005-04-22 13:00:
I think you'll find the answer to the question of where the code
that acts on the user's input lies here:

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top