20254226 实验二《Python程序设计》实验报告

20254226 2025-2026-2 《Python程序设计》实验二报告

课程:《Python程序设计》

班级: 2542

姓名: 黄婉婷

学号:20254226

实验教师:王志强

实验日期:2025年4月7日

必修/选修: 专选课

1.实验内容

(1)编写计算器程序

• 设计并完成一个完整的应用程序,完成加减乘除模等运算,功能多多益善。

• 考核基本语法、判定语句、循环语句、逻辑运算等知识点。

(2)用LLM生成一个计算器程序

• 介绍相关功能,并分析生成的程序代码含义。

• 对比分析自写程序与生成程序的区别(好与坏)。

2. 实验过程及结果

(1)编写计算器程序

• 点击项目文件旁边的+号,新建Python文件,并取名为“加减乘除模”。

image

• 学习加减乘除模整除对数的符号和形式。

加法(sum):a+b

减法(sub): a-b

乘法(multi): a*b

除法(div): a/b

取模(mod): a%b,取余数

整除(quo): a//b,只取商,不要小数

对数(log): log(a,b)其中a为真数,b为底数

• 写成函数的形式,代码如图所示:

import math
def sum4(a,b):
    return a+b
def sub4(a,b):
    return a-b
def multi4(a,b):
    return a*b
def div4(a,b):
    return a/b
def mod4(a,b):
    return a%b
def quo4(a,b):
    return a//b
def log4(a,b):
    return math.log(b,a)
print("计算机V3.0")

• 在下面增加代码,使用判定语句和循环语句。

  • 代码如下:
flag = True
while flag:
    a = eval(input("请输入a: "))
    b = eval(input("请输入b: "))
    choice = input("请选择操作:+、-、*、/、%、//、log\n")
    if choice == "+":
        result = sum4(a, b)
    elif choice == "-":
        result = sub4(a, b)
    elif choice == "*":
        result = multi4(a, b)
    elif choice == "/":
        result = div4(a, b)
    elif choice == "%":
        result = mod4(a, b)
    elif choice == "//":
        result = quo4(a, b)
    elif choice == "log":
        result = log4(a, b)
    print(f"{a}{choice}{b}={result}")
    conti = input("是否要继续?Y or N\n")
    if conti == "N" or conti == "n":
        flag = False

• 进行断点调试,查看是否有潜在错误。

  • 点击代码前面的数字,使其出现红色小圆点,右键选择调试。

image

• 无错误,整体代码如下:

import math
def sum4(a,b):
    return a+b
def sub4(a,b):
    return a-b
def multi4(a,b):
    return a*b
def div4(a,b):
    return a/b
def mod4(a,b):
    return a%b
def quo4(a,b):
    return a//b
def log4(a,b):
    return math.log(b,a)
print("计算机V3.0")
flag = True
while flag:
    a = eval(input("请输入a: "))
    b = eval(input("请输入b: "))
    choice = input("请选择操作:+、-、*、/、%、//、log\n")
    if choice == "+":
        result = sum4(a, b)
    elif choice == "-":
        result = sub4(a, b)
    elif choice == "*":
        result = multi4(a, b)
    elif choice == "/":
        result = div4(a, b)
    elif choice == "%":
        result = mod4(a, b)
    elif choice == "//":
        result = quo4(a, b)
    elif choice == "log":
        result = log4(a, b)
    print(f"{a}{choice}{b}={result}")
    conti = input("是否要继续?Y or N\n")
    if conti == "N" or conti == "n":
        flag = False

• 运行结果如下:

计算机V3.0
请输入a: 6
请输入b: 3
请选择操作:+、-、*、/、%、//、log
log
6log3=0.6131471927654585
是否要继续?Y or N
Y
请输入a: 5
请输入b: 8
请选择操作:+、-、*、/、%、//、log
%
5%8=5
是否要继续?Y or N
N

进程已结束,退出代码为 0

• 增加复数运算。

  • 出现错误,因为在Python中复数是j
请输入a: 3-3i
Traceback (most recent call last):
  File "C:\Users\LENOVO\PycharmProjects\PythonProject\加减乘除模.py", line 19, in <module>
    a = eval(input("请输入a: "))
        ~~~~^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1
    3-3i
      ^
SyntaxError: invalid decimal literal

调整并进行运行:

请输入a: 3-3j
请输入b: 4+5j
请选择操作:+、-、*、/、%、//、log
*
(3-3j)*(4+5j)=(27+3j)

(2)用LLM生成一个计算器程序

• 介绍相关功能,并分析生成的程序代码含义。

  • 功能区域:列出基础运算方式。
  print("\n【功能菜单】")
    print("1. 加法 (+)")
    print("2.减法 (-)")
    print("3. 乘法 (*)")
    print("4. 除法 (/)")
    print("5. 整除 (//)")
    print("6. 取模/余数 (%)")
    print("7. 平方 (x²)")
    print("8. 开平方 (√x)")
    print("9. 常用对数 log₁₀(x)")
    print("10. 自然对数 ln(x)")
    print("11. 指数 x^y")
    print("12. 阶乘 x!")
    print("13. 正弦 sin(x)")
    print("14. 余弦 cos(x)")
    print("15. 正切 tan(x)")
    print("16. 倒数 1/x")
    print("17. 百分比 %")
    print("0. 退出")
  • 由于运算方式过多,分单双目运算。
    # 单目运算:只需要一个数
    if choice in [7, 8, 9, 10, 12, 13, 14, 15, 16]:
        try:
            x = float(input("请输入数字 x:"))
        except ValueError:
            print("输入不是有效数字!")
            continue

        if choice == 7:
            print(f"{x}² = {x ** 2}")
        elif choice == 8:
            print(f"√{x} = {math.sqrt(x) if x >= 0 else '负数不能开平方'}")
        elif choice == 9:
            print(f"log₁₀({x}) = {math.log10(x) if x > 0 else 'x必须大于0'}")
        elif choice == 10:
            print(f"ln({x}) = {math.log(x) if x > 0 else 'x必须大于0'}")
        elif choice == 12:
            if x >= 0 and x.is_integer():
                print(f"{int(x)}! = {math.factorial(int(x))}")
            else:
                print("阶乘仅支持非负整数")
        elif choice == 13:
            print(f"sin({x}) = {math.sin(x)}")
        elif choice == 14:
            print(f"cos({x}) = {math.cos(x)}")
        elif choice == 15:
            print(f"tan({x}) = {math.tan(x)}")
        elif choice == 16:
            print(f"1/{x} = {1/x if x != 0 else '除数不能为0'}")
        continue
    # 双目运算:需要两个数
    if choice in [1, 2, 3, 4, 5, 6, 11, 17]:
        try:
            x = float(input("请输入第一个数字 x:"))
            y = float(input("请输入第二个数字 y:"))
        except ValueError:
            print("输入不是有效数字!")
            continue

        if choice == 1:
            print(f"{x} + {y} = {x + y}")
        elif choice == 2:
            print(f"{x} - {y} = {x - y}")
        elif choice == 3:
            print(f"{x} * {y} = {x * y}")
        elif choice == 4:
            print(f"{x} / {y} = {x / y if y != 0 else '除数不能为0'}")
        elif choice == 5:
            print(f"{x} // {y} = {x // y if y != 0 else '除数不能为0'}")
        elif choice == 6:
            print(f"{x} % {y} = {x % y if y != 0 else '除数不能为0'}")
        elif choice == 11:
            print(f"{x} ^ {y} = {x ** y}")
        elif choice == 17:
            print(f"{x} 的 {y}% = {x * y / 100}")
        continue

    print("无效编号,请重新输入!")
  • 总体代码如下:
import math

print("=" * 40)
print("      多功能完整版计算器")
print("=" * 40)

while True:
    print("\n【功能菜单】")
    print("1. 加法 (+)")
    print("2.减法 (-)")
    print("3. 乘法 (*)")
    print("4. 除法 (/)")
    print("5. 整除 (//)")
    print("6. 取模/余数 (%)")
    print("7. 平方 (x²)")
    print("8. 开平方 (√x)")
    print("9. 常用对数 log₁₀(x)")
    print("10. 自然对数 ln(x)")
    print("11. 指数 x^y")
    print("12. 阶乘 x!")
    print("13. 正弦 sin(x)")
    print("14. 余弦 cos(x)")
    print("15. 正切 tan(x)")
    print("16. 倒数 1/x")
    print("17. 百分比 %")
    print("0. 退出")

    try:
        choice = int(input("\n请输入功能编号:"))
    except ValueError:
        print("输入错误,请输入数字!")
        continue

    if choice == 0:
        print("计算器已退出,再见!")
        break

    # 单目运算:只需要一个数
    if choice in [7, 8, 9, 10, 12, 13, 14, 15, 16]:
        try:
            x = float(input("请输入数字 x:"))
        except ValueError:
            print("输入不是有效数字!")
            continue

        if choice == 7:
            print(f"{x}² = {x ** 2}")
        elif choice == 8:
            print(f"√{x} = {math.sqrt(x) if x >= 0 else '负数不能开平方'}")
        elif choice == 9:
            print(f"log₁₀({x}) = {math.log10(x) if x > 0 else 'x必须大于0'}")
        elif choice == 10:
            print(f"ln({x}) = {math.log(x) if x > 0 else 'x必须大于0'}")
        elif choice == 12:
            if x >= 0 and x.is_integer():
                print(f"{int(x)}! = {math.factorial(int(x))}")
            else:
                print("阶乘仅支持非负整数")
        elif choice == 13:
            print(f"sin({x}) = {math.sin(x)}")
        elif choice == 14:
            print(f"cos({x}) = {math.cos(x)}")
        elif choice == 15:
            print(f"tan({x}) = {math.tan(x)}")
        elif choice == 16:
            print(f"1/{x} = {1/x if x != 0 else '除数不能为0'}")
        continue

    # 双目运算:需要两个数
    if choice in [1, 2, 3, 4, 5, 6, 11, 17]:
        try:
            x = float(input("请输入第一个数字 x:"))
            y = float(input("请输入第二个数字 y:"))
        except ValueError:
            print("输入不是有效数字!")
            continue

        if choice == 1:
            print(f"{x} + {y} = {x + y}")
        elif choice == 2:
            print(f"{x} - {y} = {x - y}")
        elif choice == 3:
            print(f"{x} * {y} = {x * y}")
        elif choice == 4:
            print(f"{x} / {y} = {x / y if y != 0 else '除数不能为0'}")
        elif choice == 5:
            print(f"{x} // {y} = {x // y if y != 0 else '除数不能为0'}")
        elif choice == 6:
            print(f"{x} % {y} = {x % y if y != 0 else '除数不能为0'}")
        elif choice == 11:
            print(f"{x} ^ {y} = {x ** y}")
        elif choice == 17:
            print(f"{x} 的 {y}% = {x * y / 100}")
        continue

    print("无效编号,请重新输入!")
  • 运行结果如下:
【功能菜单】
1. 加法 (+)
2.减法 (-)
3. 乘法 (*)
4. 除法 (/)
5. 整除 (//)
6. 取模/余数 (%)
7. 平方 (x²)
8. 开平方 (√x)
9. 常用对数 log₁₀(x)
10. 自然对数 ln(x)
11. 指数 x^y
12. 阶乘 x!
13. 正弦 sin(x)
14. 余弦 cos(x)
15. 正切 tan(x)
16. 倒数 1/x
17. 百分比 %
0. 退出

请输入功能编号:11
请输入第一个数字 x:2
请输入第二个数字 y:78
2.0 ^ 78.0 = 3.022314549036573e+23

• 对比分析自写程序与生成程序的区别(好与坏)

优点 缺点
生成程序 ① 有更多更齐全的运算方式,可以选择更多计算方式; ② 运算方式使用中文来表示,对英语水平不高的我来说很友好; ③ 计算方式多的时候分单双目运算,降低程序出错的概率。 不能进行连续计算自写程序
自写程序 ① 简单易懂好操作。② 巧妙使用函数进行运算。 有些运算符号的英语单词不认识

3. 实验过程中遇到的问题和解决过程

问题1:关于一些基础的运算忘记了含义,还有其英文名称,比如说模。

问题1解决方案:通过网络搜索、人工智能体等方式进行学习和巩固。

问题2:运算对应的符号不太了解,如///等。

问题2解决方案:听老师上课进行解释。

问题3:Python与数学中的有些符号不同,如复数是j

问题3解决方案:上网查找资料。

其他(感悟、思考等)

通过这次实验,我学会了如何使用函数,Python中的运算方式和符号,并通过循环语句和条件语句,成功生成一个简单版的计算器,并在进一步优化中意识到Python中的复数是j这个小细节。一开始,课上我没有跟上老师的节奏,但我没有气馁,通过同学的帮助和豆包的指导,慢慢摸索出每一行代码。下次,一定先好好听课,不着急敲代码,而是要摸透其所蕴含的原理,方能做到心中有数。

参考资料

《Python程序设计》

posted @ 2026-04-08 21:42  20254226黄婉婷  阅读(5)  评论(0)    收藏  举报