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

20234111洪碧寒 2024-04-01 12:55:05

# 20234111 2023-2024-2 《Python程序设计》实验二报告

课程:《Python程序设计》
班级: 2341
姓名: 洪碧寒
学号:20234111
实验教师:王志强
实验日期:2024年3月31日
必修/选修: 公选课

## 1.实验内容

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

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

创建工程项目,使用Python语言实现具体的操作运算,并完成程序调试和运行,代码托管到码云。

注:在华为ECS服务器(OpenOuler系统)和物理机(Windows/Linux系统)上使用VIM、IDLE、Pycharm等工具编程实现。

## 2. 实验过程及结果

1. 首先,确定应用程序的功能和需求。在这个例子中,我们需要实现基本的四则运算(加、减、乘、除)和取模运算。

2. 点击项目开始根据要求编程

3. 实现运算功能。根据用户输入的操作符和操作数,进行相应的运算。可以使用条件语句或者函数来实现不同的运算。

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 "Error: Division by zero is not allowed"
    return x / y


def modulo(x, y):
    if y == 0:
        return "Error: Modulo by zero is not allowed"
    return x % y


def main():
    print("Welcome to the Basic Calculator App!")
    while True:
        try:
            num1 = float(input("Enter the first number: "))
            num2 = float(input("Enter the second number: "))
            operator = input("Enter the operator (+, -, *, /, %): ")

            if operator == '+':
                print(f"{num1} + {num2} = {add(num1, num2)}")
            elif operator == '-':
                print(f"{num1} - {num2} = {subtract(num1, num2)}")
            elif operator == '*':
                print(f"{num1} * {num2} = {multiply(num1, num2)}")
            elif operator == '/':
                print(f"{num1} / {num2} = {divide(num1, num2)}")
            elif operator == '%':
                print(f"{num1} % {num2} = {modulo(num1, num2)}")
            else:
                print("Error: Invalid operator. Please try again.")

        except ValueError:
            print("Error: Invalid input. Please enter valid numbers.")
        except ZeroDivisionError:
            print("Error: Division by zero is not allowed.")

        again = input("Do you want to perform another calculation? (yes/no): ")
        if again.lower() != 'yes':
            break


if __name__ == "__main__":
    main()

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

问题1:bug过多

问题1解决方案:询问同学并且对照CSDN相关教程学习

问题2:代码经常有缩进格式错误

问题2解决方案:仔细寻找检查

问题3:引号总是英文中文输入错误

问题3解决方案:仔细查找检查

## 其他(感悟、思考等)

程序编写是一个严谨且细致的工作,不能输入错一个信息,否则后面查找会非常麻烦和复杂。这就需要我们养成良好的习惯,在输入时就减少出错,以减少后期查找错误时不必要的麻烦。

参考资料

《零基础学Python》

...全文
63 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

93

社区成员

发帖
与我相关
我的任务
社区描述
Python程序设计作业
软件构建 高校 北京·丰台区
社区管理员
  • blackwall0321
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧