Python Basic Learning Code errors and exceptions

Source: Internet
Author: User
Def func1 (): Try:return float (' abc ') except Valueerror,e:print edef Func2 (): Try:astr = '        ABC ' float (astr) except valueerror:astr = None return astrdef func3 (): TRY:ASTR = ' abc ' Float (astr) except valueerror:astr = ' count not convert Non-number to float ' return astrdef safe_float ( argment): Try:retval = float (argment) except Valueerror:retval = ' count not ' convert Non-number to FL    Oat ' except Typeerror:retval = ' object type cannot is convert to float ' return retvaldef func4 (argment): Try:retval = float (argment) except (valueerror,typeerror): retval = ' argment must be a number or numeri  C String ' Return Retvaldef Func5 (argment): Try:retval = float (argment) except Valueerror,e:print    e print type (e) Print e.__class__ print e.__class__.__doc__ print e.__class__.__name__def func6 (argment): Try:retval = Float (argment) except (valueerror,typeerror), e:retval = str (e) return retvaldef main (): ' Handles all the DA Ta processing ' log = open (' E:\\cardlog.txt ', ' w ') Try:ccfile = open (' E:\\cardlog.txt ', ' r ') Txns = CCFI Le.readlines () except Ioerror,e:log.write (' no txns this month\n ') log.close () return ccfile.cl OSE () Total = 0.00 Log.write ("Account log:\n") for eachtxn in Txns:result = Func6 (EACHTXN) if Isin Stance (result,float): Total + = result Log.write (' data...processed\n ') ELSE:LOG.WR Ite (' ignored:%s '%result) print ' $%.2f newbalance '% Total log.close () #if __name__ = = ' __main__ ': # main () def Func7        (): assert 1 = = 0def Func8 (): try:assert 0 = = 1, ' One does not equal zero ' except Assertionerror,e: print '%s:%s '% (e.__class__.__name__,e) #assertdef func9 (expr,args=none): If __debug__ and not expr:raise Asse Rtionerror,argsdef func10 (): try:float (' abc ') except:import sys exect = Sys.exc_info () return exectdef FUNC11 (        ): try:f = open (' test.txt ') except:return None else:return fdef func12 (): Try:    Raw_input (' Input data: ') except (eoferror,keyboardinterrupt): Return noneimport math,cmathdef safe_sqrt (data): Try:ret = math.sqrt (data) except Valueerror:ret = cmath.sqrt (data) return Retimport sysdef func13 (         ): Try:s = Raw_input (' Enter something--> ') except Eoferror:print ' \nwhy did you do a EOF on me? '    Sys.exit (0) except:print ' \nsome error/exception occurred. ' print ' Done ' func13 ()

The above is the Python Basic Learning Code error and exception content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.