20231425 李泽儒《Python程序设计》实验二报告

20231425李泽儒《Python程序设计》实验二报告

课程:《Python程序设计》
班级: 2314
姓名: 李泽儒
学号:20231425
实验教师:王志强
实验日期:2022年3月27日
必修/选修: 公选课

1. 实验内容

1.设计并完成一个完整的应用程序,完成加减乘除模等运算,功能多多益善。
2. 考核基本语法、判定语句、循环语句、逻辑运算等知识点。

2.实验过程及结果

步骤一:编写计算器
源代码如下:

解释:

import math

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):
    """除法"""
    if y == 0:
        return "除数不能为0"
    else:
        return x / y

def exponentiation(x, y):
    """指数"""
    return x ** y

def logarithm(x, base):
    """对数"""
    return math.log(x, base)

def sin(x):
    """正弦"""
    return math.sin(math.radians(x))

def cos(x):
    """余弦"""
    return math.cos(math.radians(x))

def tan(x):
    """正切"""
    return math.tan(math.radians(x))

# 主程序
print("选择运算:")
print("1.加")
print("2.减")
print("3.乘")
print("4.除")
print("5.指数")
print("6.对数")
print("7.正弦")
print("8.余弦")
print("9.正切")

while True:
    choice = input("请输入选择(1/2/3/4/5/6/7/8/9): ")

    if choice in ('1', '2', '3', '4', '5', '6'):
        num1 = float(input("输入第一个数字: "))
        num2 = float(input("输入第二个数字: "))

        if choice == '1':
            print("结果:", add(num1, num2))
        elif choice == '2':
            print("结果:", subtract(num1, num2))
        elif choice == '3':
            print("结果:", multiply(num1, num2))
        elif choice == '4':
            print("结果:", divide(num1, num2))
        elif choice == '5':
            print("结果:", exponentiation(num1, num2))
        elif choice == '6':
            base = float(input("输入对数的底数: "))
            print("结果:", logarithm(num1, base))
    elif choice in ('7', '8', '9'):
        angle = float(input("输入角度: "))

        if choice == '7':
            print("结果:", sin(angle))
        elif choice == '8':
            print("结果:", cos(angle))
        elif choice == '9':
            print("结果:", tan(angle))
    else:
        print("无效输入")
    
    again = input("继续运算?(是/否): ")
    if again.lower() != '是':
        break


步骤二:运行调试:

 步骤三:上传代码至gitee

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

  1. 除数为零处理: 在除法运算中,需要考虑到用户可能输入零作为除数的情况,这时程序应该给出合适的提示而不是直接抛出错误。

  2. 对数运算参数合法性: 对于对数运算,需要确保用户输入的底数大于零,否则会产生错误。

  3. 角度单位转换: 三角函数运算通常要求输入的角度为弧度而非角度,所以程序中需要进行正确的单位转换。

  • 40
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值