Python Learning note Five: errors, debugging, and testing

Source: Internet
Author: User
Tags assert greatest common divisor

1 #!/usr/bin/env python2 #Coding:utf-8 # Encoding3 4 'a test module'5 6 __author__='Sunflower'    7 8 9 #exception Error TypeTen #assertionerror Segmentation Errors One Try: AMy_list =['Zhangsan'] -     assertLen (my_list) >0 - My_list.pop () the     assertLen (my_list) >0 - exceptassertionerror as Reason: -     Print('Segmentation Errors'+str (reason)) -  +  - #Indexerror + #keyerror Dictionary Access # my_dict.get (' four ') A #IOError input/output operation error at #OSError Operating system error - #overflowerror operation exceeded limit - #SyntaxError # syntax - #TraceError - #zerodivisionerror except 0 - #TypeError Type Error in  - #Debug Try ... except ... finally raise to Try:#try to error first print, followed by instruction not +f = open ('I am a file. txt','R') -     PrintF.read () the f.close () * exceptIOError as Reason: $     Print('File Error'+str (reason))Panax Notoginseng finally:#Absolute Execution - f.close () the  + RaiseIOError ('File Error') A  the #Else + #If ... else ... Or... Or ... - #While...else ... Finish the job ... Do not finish ... $ #find greatest common divisor $ defshow_factor (num): -Count = num//2#except 2 -      whileCount > 1: the         ifNum% Count = =0: -             Print('%d The largest approximate number is%d'%(num, count))Wuyi              Break theCount-= 1 -     Else: Wu         Print('%d is the prime number!! '% num)#Execution is all done and executed. -  Aboutnum = Int (33) $ Print(Show_factor (num)) - #try...else ... - Try: -int'ABC') A exceptvalueerror as Reason: +     Print('something went wrong.'+str (reason)) the Else: -     Print('There is no error.') $  the # with the Try: theOpen'Data.txt','W') the      forEach_lineinchF: -         Print(Each_line) in exceptIOError: the     Print('something went wrong.') the finally: AboutF.close ()

Python Learning note Five: errors, debugging, and testing

Related Article

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.