Home > other >  Python written arithmetic, using the try exception handling
Python written arithmetic, using the try exception handling

Time:10-06

code below

This is to improve another BBS main code
Connection is as follows: https://blog.csdn.net/honeygirl_/article/details/91612947

 
# define functions
The class myCalc:
Def __init__ (self, num_a num_b) :
Self. Num_a=num_a
Self. Num_b=num_b
# Numbers together
Def add (self, Retain) :
Return round (self. Num_a + self. Num_b, Retain)
# digital subtraction
Def sub (self, Retain) :
Return round (self. Num_a - self. Num_b, Retain)
# Numbers multiplied
Def the mul (self, Retain) :
Return round (self. Self num_a *. Num_b, Retain)
# digital division by
Def div (self, Retain) :
Return round (self. Num_a/self. Num_b, Retain)


# while True implementation cycle
While True:
Try:
# enter the required calculation of digital
Get_num1=input (" please enter the first number: ")

# enter arithmetic symbols + - */
Opera=input (" please enter the operator: ")

# enter the second number of calculations required
Get_num2=input (" please enter the second number: ")

# enter a decimal point digits
Get_retain=input (" please enter retain the decimal digits: ")


# into a floating point number
Num1=float (get_num1)
Num2=float (get_num2)
Retain=int (get_retain)
# define variables, the initial value of 0
Result=0.0

# judge input algorithms symbol
If the opera=="+" :
Result=myCalc (num1, num2). The add (retain)
Print (" output is ", the result, '\ n')
Elif opera=="-" :
Result=myCalc (num1, num2). Sub (retain)
Print (" output is ", the result, '\ n')
Elif opera=="*" :
Result=myCalc (num1, num2). The mul (retain)
Print (" output is ", the result, '\ n')
Elif opera=="/" :
Result=myCalc (num1, num2). Div (retain)
Print (" output is ", the result, '\ n')
The else:
Print (" please input the correct mathematical symbol "+, -, *,/'\ n")

Except:
Print (" input incorrect \ n ")

The continue



Processing the input string with exception handling error, to be more perfect, there is wrong place still please correct

CodePudding user response:

Registered a really trouble
  • Related