20193117 Experiment 2 "Python Programming" Experiment Report

Student ID 2019-2020-2 "Python Programming" Experiment x Report

Course: "Python Programming"
Class: 1931
Name: Wei Haoming
Student ID: 20193117
Experimental Teacher: Wang Zhiqiang
Experiment Date: April 11, 2020
Compulsory / Elective: Public Elective Course

1. Experimental content

  • Design and complete a complete application program, complete operations such as addition, subtraction, multiplication and division, with many functions and benefits
  • Examine the knowledge points of basic grammar, judgment statements, loop statements, logical operations, etc.

2. Experimental process and results

By defining the functions of addition, subtraction, multiplication, and division for each calculation method, the user selects the input calculation method, then enters the number, and completes the calculation by calling the main function

Code

def add(x,y):

    return x + y
def subtract(x,y):

    return x - y
def multiply(x,y):

    return x * y
def divide(x,y):

    return x / y
while(1):
    print("是否进行计算:")
    choice = input("是/否:")
    if choice == '否':
        break
    elif choice == '是':

        print("输入想进行的步骤(数字)")
        print("1.加法\n2.减法\n3.乘法\n4.除法:")
        n = int(input())
        num1 = int(input("输入第一个数:"))
        num2 = int(input("输入第二个数:"))

        if n == 1:
            print(num1, '+', num2, '=', add(num1, num2))

        elif n == 2:
            print(num1, '-', num2, '=', subtract(num1, num2))

        elif n == 3:
            print(num1, 'X', num2, '=', multiply(num1, num2))

        elif n == 4:
            print(num1, '÷', num2, '=', divide(num1, num2))

Code cloud link:

Link .

3. Problems encountered during the experiment and the resolution process

  • Question 1: Consider how to make the program perform multiple calculations
  • Problem one solution: operation through loop multiple times

Others (sentiment, thinking, etc.)

There are many ways to program the Python program. For example, the experiment can be judged by a function or directly by an if statement. You can also use characters to store the previous calculation result and perform a second operation.

References

Guess you like

Origin www.cnblogs.com/whm1129/p/12733937.html