20191305 2020-2021-2 《Python程序设计》实验二报告
课程:《Python程序设计》
班级: 1913
姓名: 李天琦
学号:20191305
实验教师:王志强
实验日期:2021年5月9日
必修/选修: 公选课
1.实验内容
设计并完成一个完整的应用程序,完成加减乘除模等运算,功能多多益善。
考核基本语法、判定语句、循环语句、逻辑运算等知识点
2. 实验过程及结果
# 定义函数
import math
def add(x, y):
"""相加"""
return x + y
def sub(x, y):
"""相减"""
return x - y
def mul(x, y):
"""相乘"""
return x * y
def div(x, y):
"""相除"""
return x / y
def pow(x, y):
"""计算x的y次方"""
return math.pow(x, y)
def hyp(x, y):
"""计算平方和"""
return math.hypot(x, y)
def sqr(x):
"""平方根"""
return math.sqrt(x)
while True:
print("请选择计算公式:")
print("1.加法"
"2.减法"
"3.乘法"
"4.除法"
"5.x的y次方"
"6.平方和"
"7.平方根")
choice = int(input())
if choice == 1 or choice==2 or choice==3 or choice==4 or choice==5 or choice == 6:
x = int(input("请输入第一个数:"))
y = int(input("请输入第二个数:"))
if choice == 1:
print(add(x, y))
elif choice == 2:
print(sub(x, y))
elif choice == 3:
print(mul(x, y))
elif choice == 4:
print(div(x, y))
elif choice == 5:
print(pow(x, y))
else:
print(hyp(x, y))
elif choice == 7:
x = int(input("请输入要开方的数:"))
print(sqr(x))
else:
print("输入错误,请重新输入!")
choice2 = int(input("是否继续?1.继续 2.停止"))
if choice2 == 1:
continue
else:
break
![]()
3. 实验过程中遇到的问题和解决过程
对于函数的运用还不是非常熟练,还需要加强练习,对于编写一个小程序需要尽可能想到可能发生的问题,以及用户的输入错误,做到有防护性,这点还需要以后加强学习。利用函数可以使代码更简洁更清晰,同时利用率高。


浙公网安备 33010602011771号