I noticed that when i posted programs on here people gave me their versions, and had

import *

def somethng_here():
    while True:
        Try:
            pass
    except ValueError #or some other types of things liek that

I don't understand how a function can be FALSE, as well as how these except errors...if you will, work. Can someone explain or link me to a clear tutorial :)

Edited 6 Years Ago by ffs82defxp: n/a

What Is an Exception?

Python uses exception objects.
When it encounters an error, it raises an exception.
If such an exception object is not handled (or caught), the program
terminates with a so-called traceback (an error message)

>>> b

Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    b
NameError: name 'b' is not defined
>>>
import exceptions
>>> dir(exceptions)
<list method in exception class>

So let try it out. Version 1

x = input('Enter the first number: ')
y = input('Enter the second number: ')
print x/y 
'''
Enter the first number: 10
Enter the second number: 5
2
'''

Work just fine. Version 2

x = input('Enter the first number: ')
y = input('Enter the second number: ')
print x/y
'''
Enter the first number: 10
Enter the second number: 0
Traceback (most recent call last):
  File "E:\1py\Div\dffddd.py", line 3, in <module>
    print x/y
ZeroDivisionError: integer division or modulo by zero
'''

Here we got an problem,user did divided 10/0.
So what to do now?
We have to catch that exceptions.
You do this with the try/except statement. Version 3

try:
    x = input('Enter the first number: ')
    y = input('Enter the second number: ')
    print x/y
except ZeroDivisionError:
    print "Dont divid by zero!"
'''
Enter the first number: 10
Enter the second number: 0
Dont divid by zero!
'''

Look we used ZeroDivisionError from error message,and now we dont get and error message.
A new problem now the program exit after the print statement.
we have to use a loop. Version 4

while True:    
    try:        
        x = input('Enter the first number: ')
        y = input('Enter the second number: ')
        print x/y
        break  #we break out when no problem
    except ZeroDivisionError:
        print "Dont divid by zero,try again"
'''
Enter the first number: 10
Enter the second number: 0
Dont divid by zero,try again
Enter the first number: 10
Enter the second number: 5
2
'''

Work just fine.

So a user that are not so bright or let say a type error.
Hi/she type a as first number.
Enter the first number: a Version 5

while True:    
    try:        
        x = input('Enter the first number: ')
        y = input('Enter the second number: ')
        print x/y
        break  #we break out when no problem
    except ZeroDivisionError:
        print "Dont divid by zero,try again"
'''
Enter the first number: a
Traceback (most recent call last):
  File "E:\1py\Div\hjj.py", line 3, in <module>
    x = input('Enter the first number: ')
  File "<string>", line 0, in <module>
NameError: name 'a' is not defined
'''

Now a new error to catch(NameError)
Now just place it behind ZeroDivisionError. Version 5

while True:    
    try:        
        x = input('Enter the first number: ')
        y = input('Enter the second number: ')
        print x/y
        break  #we break out when no problem
    except (ZeroDivisionError,NameError):
        print "Your input was wrong,try again"
'''
Enter the first number: a
Your input was wrong,try again
Enter the first number: 10
Enter the second number: 5
2
'''

Works just fine.

So they last one, we make fuction with a new error that i catch.
And fix divided with float so it calulate correct.
And a new output %.2f (means float 2 decimal number after .) Version 6

def num_divid():
    '''Function to divided two number'''
    while True:    
        try:        
            x = float(input('Enter the first number: '))
            y = float(input('Enter the second number: '))
            return '%s divided by %s is %.2f ' % (x, y, x/y)           
        except (ZeroDivisionError,NameError,SyntaxError):
            print "Your input was wrong,try again"            

print num_divid()
'''
Enter the first number: 10.7
Enter the second number: 5.478
10.7 divided by 5.478 is 1.95 
'''

More on this.
http://docs.python.org/tutorial/errors.html

Edited 6 Years Ago by snippsat: n/a

ooh thats so cool, one question. where can i get a list of all the error exceptions and what they do because this can come in handy a lot to me.

http://docs.python.org/library/exceptions.html
http://python.about.com/od/pythonstandardlibrary/a/lib_exceptions.htm
Use python shell as postet on top.

>>> import exceptions
>>> dir(exceptions)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'EnvironmentError', 'Exception', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '__doc__', '__name__', '__package__']
>>>
This question has already been answered. Start a new discussion instead.