20193417 2019-2020-2 "Python Programming" Experiment 2 Report

20193417 2019-2020-2 "Python Programming" Experiment 2 Report

  • Course: "Python Programming"

  • Class: 1934

  • Name: Duan Kaiyu

  • Student ID: 20193417

  • Experimental teacher: Wang Zhiqiang

  • Date of experiment: April 11, 2020

  • Compulsory / Elective: Public elective courses

1. Experimental content

  • Design and complete a complete application program, complete operations such as addition, subtraction, multiplication and division, and more functions.

  • Examine the knowledge points of basic grammar, judgment statements, loop statements, logical operations, etc.

2. Experimental process and results

1. Process

1. First create a Cal () class for calculation, and define the attribute result in which the operation results are stored, for addition, subtraction, multiplication and division, square, nth power, take the reciprocal, take the percent sign, and clear the method

 

 

 

2. Define the variable ult_result as the object, use while True to perform multiple operations in an infinite loop, let the user specify the arithmetic operator by number, and judge the operation required by the user through the if-else statement

 

 

 

 

3. When the user enters characters, use the break statement to jump out of the loop and end the calculation

 

Source code:


#Define calculation class class Cal: 
 result = 0 
 def plus (self, num1): 
     #addition self.result + = num1 
     print ('=', self.result) 

 def minus (self, num1): 
     #subtraction self.result- = num1 
     print ('=', self.result) 
 def times (self, num1): 
     #Multiplication self.result * = num1 
     print ('=', self.result) 
 def devide (self, num1): #Division 
     try: 
         self.result / = num1 
     except: 
         print ("MathError!") # Can't divide by zero 
         self.result = 0 
     print ('=', self.result) 
 
 def square (self, num1): 
     #square self.result = pow (num1,2) 
     print ('=', self.result)
 def nth_power (self, num1, num2): #nth power 
     self.result = pow (num1, num2) 
     print ('=', self.result) 
 def reciprocal (self, num1): 
     #take the countdown self.result = 1 / num1 
     print ('=', self.result) 
 def percent_sign (self, num1): 
     #Percent sign self.result = num1 * 0.01 
     print ('=', self.result) 
 def clear (self): #Clear data 
     self.result = 0 
     print ('0') 

opr = {1: '+', 2: '-', 3: '*', 4: '÷', 5: 'x²', 6: 'x ^ n ', 7:' 1 / x ', 8:'% ', 9:' CLEAR ',' p ':' () '} 
#User input code, how to return the corresponding symbol immediately numget1 = float (input (' Calculation start \ n ')) 
ult_result = Cal () 
ult_result.result = numget1
while True:
 print ('=' * 20, "\ n1. + \ t2.-\ t3. * \ n4. ÷ \ t5. x² \ t6. x ^ n \ n7. 1 / x \ t8.% \ t9. CLEAR \ n'p ': () \ n ",' = '* 20) 
 try: 
     signget1 = int (input ()) 
     print (ult_result.result, opr [signget1]) 
     if 1 <= signget1 <= 4: 
         numget2 = input () 
         if numget2 == 'p': #if the bracket is entered 
             print ('()') 
             num_emptybox = ult_result.result #store the original value 
             sign_emptybox = signget1 #store the original operator 
             ult_result.result = float (input ( )) 
             print ('=' * 20, "\ n1. + \ t2.-\ t3. * \ n4. ÷ \ t5. x² \ t6. x ^ n \ n7. 1 / x \ t8.% \ t9. CLEAR \ n'p ': () \ n ",' = '*20)
             signget1 = int(input())*20)
             print(ult_result.result,opr[signget1])
             if (1<=signget1) and (signget1 <= 4):
                 numget2 = float(input())
             if signget1 == 1 :
                 print(ult_result.result,'+',numget2)
                 ult_result.plus(numget2)
             elif signget1 == 2:
                 print(ult_result.result,'-',numget2)
                 ult_result.minus(numget2)
             elif signget1 == 3:
                 print(ult_result.result,'x',numget2)
                 ult_result.times(numget2)
             elif signget1 == 4:
                 print(ult_result.result,'÷',numget2)
                 ult_result.devide(numget2)
             elif signget1 == 5: 
                 ult_result.square(ult_result.result)
             elif signget1 == 6: 
                 nth = float (input ("n =")) 
                 ult_result.nth_power (ult_result.result, nth) 
             elif signget1 == 7: 
                 ult_result.reciprocal (ult_result.result) 
             elif signget1 == 8: 
                 ult_result.percent_sign (ult_result.result) 
             num2_emptybox = ult_result.result ## Store new 
             valueult_result.result = num_emptybox ## Recall the original value 
             signget1 = sign_emptybox #Recall the original operator 
             numget2 = num2_emptybox ## Call 
         numget2 = float (numget2) 
     if signget1 == 1 for the new value : 
         print (ult_result.result, '+', numget2)
         ult_result.plus(numget2)       
     elif signget1 == 2:
         print(ult_result.result,'-',numget2)
         ult_result.minus(numget2)
     elif signget1 == 3:
         print(ult_result.result,'x',numget2)
         ult_result.times(numget2)
     elif signget1 == 4:
         print(ult_result.result,'÷',numget2)
         ult_result.devide(numget2)
     elif signget1 == 5:
         ult_result.square(ult_result.result)
     elif signget1 == 6:
         nth = float(input("n = "))
         ult_result.nth_power(ult_result.result, nth)
     elif signget1 == 7:
         ult_result.reciprocal(ult_result.result)
     elif signget1 == 8:
         ult_result.percent_sign(ult_result.result)
     elif signget1 == 9:
         ult_result.clear()
         continue
 except :
     print('计算结束')    
     break

2. Results

1. Take 1 + 2 = 3 as an example

 

 

2. Take 3 x (5-2) = 9 as an example

 

 

3. Take 5³ + 1/5 = 125.2 as an example

 

 

 


3. Problems encountered during the experiment and the resolution process

  • Question 1: In division, 0 cannot be a divisor, otherwise an error will be reported, how to set the error reporting mechanism?

    • Solution to Problem 1: Use try / except statement to prompt "MathError!"

  • Question 2: After the user specifies the operator, how to quickly return the corresponding symbol?

    • Solution to Problem 2: Use a dictionary to set 1 ~ 9 and 'p' as index values, set the operators "+", "-", etc. to the corresponding values, and use the specified number entered by the user as the index value. Read and output the content corresponding to the dictionary

      opr = {1:'+',2:'-',3: '*',4: '÷',5: 'x²',6: 'x^n',7: '1/x',8: '%',9: 'CLEAR','p': '()'}
      signget1 = int(input())
      print(ult_result.result,opr[signget1])
      

        

  • Question 3: How do I give the user the right to use parentheses?

    • Solution to Problem 3: Use the character 'p' as the specified symbol in parentheses, when the user enters the second arithmetic number, use the if statement to determine whether the user has entered p

      if numget2 == 'p': #If the brackets are entered 
            print ('()')
      

        

       

      If parentheses are used, the arithmetic numbers entered previously (stored in ult_result.result) and the operator specified number signget1 should be stored in a new "container"-num_emptybox, sign_emptybox, and recalled after executing the content in parentheses

      num_emptybox = ult_result.result #store the original value 
      sign_emptybox = signget1 #store the original operator 
      # omit the calculation process in 
      parenthesesnum2_emptybox = ult_result.result ## store the new 
      valueult_result.result = num_emptybox ## recall the original value 
      signget1 = sign_emptybox #Recall the original operator 
      numget2 = num2_emptybox ## Call the new value
      

        

Others (sentiment, thinking, etc.)

The code for writing a calculator is much more complicated than imagined. In the case of a large number of lines of code, always pay attention to check whether the code has errors, otherwise it is difficult to check the errors when running, so the use of debug is also to Important. There are still many areas for improvement in the program I wrote. For example, when using the if-else statement many times to determine what operator the user wants to choose, a function can be set up to have such a function to uphold the programming world "DRY" (Don) 't repeat yourself).

I looked at the calculator program written by Great God on the Internet, which has more than 300 lines, and the running terminal can generate a visual window, which is very advanced. I want to learn, but unfortunately my level is limited and I can't understand his program. I can only write the low-end calculator program by myself. Only after I knew that my skills were not as good as others did I feel that I had to work harder.


References

Guess you like

Origin www.cnblogs.com/dky2118843269/p/12709328.html