This article is 【Python Language foundation 】 Column articles , Mainly the notes and exercises in class
Python special column Portal
The experimental source code has been in Github Arrangement
Define a class that implements queues using lists List_Queue, Queue elements can be entered 、 Delete 、 Find the queue length and other functions
Define an exception handling class List_Queue_Exception Antitype List_Queue Handle the possible exceptions in
queue (queue) It is only allowed to insert at one end , A linear table with deletion at the other end . A first in, first out (First In First Out) The linear table , abbreviation FIFO. The end allowed to be inserted is called the end of the queue , The end allowed to be deleted is called the team leader .
Through the design List_Queue class , Save queue data with collections , increase list.append()
,list.pop()
Design List_Queue_Exception class , When the queue is empty and an outbound operation is performed , Handle exceptions
""" @Author: Zhang Shier @Date:2022 year 06 month 10 Japan @CSDN: Zhang Shier @Blog:zhangshier.vip """
class List_Queue:
# initialization
def __init__(self):
self.list1 = [ ]
print ( ' Successful initialization !' )
# The team
def enqueue(self, item):
self.list1.append ( item )
print ( ' Add success !' )
# Out of the team
def dequeue(self):
if len ( self.list1 ) > 0:
print ( " Out of queue data :", self.list1[ 0 ] )
self.list1.pop ( 0 )
else:
raise List_Queue_Exception ()
# Return queue length
def lenqueue(self):
return len ( self.list1 )
# Output queue
def l_queue(self):
print ( self.list1 )
class List_Queue_Exception ( BaseException ):
def __init__(self):
print ( " The list is empty. !" )
if __name__ == '__main__':
list_queue = List_Queue ()
print ( "-----------------" )
print ( "*****1, The team *****" )
print ( "*****2, Out of the team *****" )
print ( "*****3, The queue length *" )
print ( "*****4, Display list *" )
print ( "*****0, sign out *" )
print ( "-----------------" )
while True:
x = int ( input ( " Enter the serial number :" ) )
try:
if x in [ 0, 1, 2, 3, 4 ]:
if x == 0:
print ( " Has been withdrawn from " )
break;
elif x == 1:
y = input ( " Please enter the entered data :" )
list_queue.enqueue ( y )
elif x == 2:
list_queue.dequeue ()
elif x == 3:
print ( " The queue length is :", list_queue.lenqueue () )
elif x == 4:
list_queue.l_queue ()
else:
print ( " Incorrect input !" )
except BaseException as ex:
print ( ex )
Define a class that implements arithmetic operations Arithmetic_Operation, You can add two integers 、 Subtraction 、 Multiplication and division
Define a test class Test_Arithmetic_Operation Yes Arithmetic_Operation Test the function of
stay Arithmatic_Operation
Class 、add()
、sub()
、mul()
、div()
, stay Test_Arithmetic_Operation
Class using the unit test framework unittest
Yes Arithmatic_Operation
Class test
""" @Author: Zhang Shier @Date:2022 year 06 month 10 Japan @CSDN: Zhang Shier @Blog:zhangshier.vip """
# utilize unittest Framework testing , File names should not contain special symbols ".、,?"
import unittest
class Arithmatic_Operation:
def add(self):
return self.x + self.y
def sub(self):
return self.x - self.y
def mul(self):
return self.x*self.y
def div(self):
return self.x/self.y
def __init__(self, x, y):
self.x = x
self.y = y
class Test_Arithmetic_Operation ( unittest.TestCase ):
def setUp(self):
self.op = Arithmatic_Operation ( 3, 5 )
def test_add(self):
if self.assertEqual ( self.op.add (), 8 ):
print ( " correct " )
def test_sub(self):
self.assertEqual ( self.op.sub (), -2 )
def test_mul(self):
self.assertEqual ( self.op.mul (), 15 )
def test_div(self):
self.assertEqual ( self.op.div (), 0.6 )
if __name__ == '__main__':
unittest.main ()