93
社区成员
# 20234103 2023-2024-2 《Python程序设计》实验2报告
课程:《Python程序设计》
班级: 2341
姓名: 徐宇衡
学号:20234103
实验教师:王志强
实验日期:2023年3月26日
必修/选修: 公选课
## 1.实验内容
设计并完成一个完整的应用程序,完成加减乘除模等运算,功能多多益善。
## 2. 实验过程及结果
Step1.使用def.定义新的函数,列举定义加减乘除的及复数的运算:有sun(),sub(),mul(),div(),log(),cdiv(),csub(),cmul等。
明晰后简单编写一下用法:
def sum(a,b):
return(a+b)
def sub(a,b):
return(a-b)
def mul(a,b):
return(a*b)
def div(a,b):
If b==0:
print(“0不能为除数”)
return 0
return (a/b)
Step2.运用if函数进行循环结构的应用,while flag函数的引用实现重复运算。
接上文:
operator = input("请输入运算符号(+-*/):")
if operator == "+":
a = eval(input("请输入a:"))
b = int(input("请输入b:"))
print("a+b=",sum(a, b))
elif operator == "-":
a = eval(input("请输入a:"))
b = int(input("请输入b:"))
print("a-b=",sub(a, b))
elif operator == "*":
a = eval(input("请输入a:"))
b = int(input("请输入b:"))
print("a*b=",mul(a, b))
elif operator == "/":
a = eval(input("请输入a:"))
b = int(input("请输入b:"))
print("a/b=",div(a, b))
print("欢迎使用besti计算器")
print("开发者:徐宇衡")
print("开发日期:2024.4.1")
Step3.编程结束,开始运行。
添加序列
coperator=input("请输入复数运算/+-*;")
c1 =eval(input("请输入复数1(eg:i+2j):"))
c2 =eval(input("请输入复数3(eg:i+2g):"))
if coperator =="/":
print("(",c1,")/(",c2,")=",cdiv(c1,c2))
elif coperator == "+":
print("(", c1, ")+(", c2, ")=", csum(c1, c2))
elif coperator == "-":
print("(", c1, ")-(", c2, ")=", csub(c1, c2))
elif coperator == "*":
print("(", c1, ")*(", c2, ")=", cmul(c1, c2))
3.提交文件到gitee
## 3. 实验过程中遇到的问题和解决过程
问题一.需格外注意对除法运算的编程,除数不能为零
def div(a,b):
If b==0:
print(“0不能为除数”)
return 0
return (a/b)
问题二.flag=true需要大写,否则无法运算。
问题三.不熟悉运算的相关知识
解决方案:查找学习通课件,import math
## 4. 心得体会
程序的编写一定要严谨细致,特别注意某些特殊情况,并及时在代码中显示;
对于基本的if-else语句不够熟悉,导致在运行过程中出现错误。
及时复习巩固相关知识,以达到较熟练的应用。