20233324 周浩然 2023-2024-2 《Python程序设计》实验2报告

20233324周浩然 2024-03-30 23:11:42

课程:《Python程序设计》
班级: 2333班
姓名: 周浩然
学号:20233324
实验教师:王志强
实验日期:2024年3月27日
必修/选修: 公选课

一、实验内容

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

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

二、实验要求

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

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

三、实验内容及结果

(1)完整实验代码:
设计了两个功能可以供用户选择使用:1.计算器功能 2.随机加减乘除题目及计分

import math
import random

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 b
    return round((a / b), 2)

def mod(a, b):
    return(a % b)

def pysin(a):
    return math.sin(a)

def pycomplex(a,b):
    complexOper = input("请输入复数的运算:+ - * /")
    if complexOper == "+":
        print("a + b = ",a+b)
        return a + b
    elif complexOper == "-":
        print("a - b = ",a-b)
        return a - b
    elif complexOper == "*":
        print("a * b = ",a*b)
        return a * b
    elif complexOper == "/":
        print("a / b = ",a/b)
        return a/b

def pylog(a):
    return math.log10(a)
def pylog_a_b(a,b):
    return math.log(b) / math.log(a)


print("====================欢迎使用BestI计算系统===================")
print("**                  作者:20233324周浩然                 **")
print("**                  开发时间: 2024.03.27                **")
print("**                  资助通道:www.1669012843@qq.com      **")
print("=========================================================")

print("1.简单计算器")
print("2.加减乘除训练")

choice = int(input("请选择功能:"))
if choice == 1 :
    exitflag = True
    while exitflag == True:

        a = eval(input("请输入a: "))

        b = eval(input("请输入b: "))

        operator = input("请输入运算符(+ - * / mod sin com log): ")

        if operator == "+":
            print("a + b = ", sum(a, b))
        elif operator == "-":
            print("a - b = ", sub(a, b))
        elif operator == "*":
            print("a * b = ", mul(a, b))
        elif operator == "/":
            print("a / b = ", div(a, b))
        elif operator == "mod":
            print("a mod b = ", mod(a,b))
        elif operator == "sin":
            print("sin(a) = ",pysin(a),"sin(b) = ",pysin(b))
        elif operator == "com":
            pycomplex(a,b)
        elif operator == "log":
            print("lg(a) = ",pylog(a),"lg(b) = ",pylog(b),"以a为底b的对数为: ",pylog_a_b(a,b))

        exitflag = True if input("是否需要继续使用计算器:Y or N : ") == "Y" else False
elif choice == 2 :
    print("注意:除法保留两位小数")
    tough = 0
    choice_tough = int(input("请选择难度:\n 1. 0 - 100\n 2. 0 - 1000\n 3. 0 - 10000\n"))
    if choice_tough == 1:
        tough = 101
    elif choice_tough == 2:
        tough = 1001
    elif choice_tough == 3:
        tough = 10001
    count = int(input("您期望训练多少道加减乘除题目? : "))
    count_real = count
    score = 0
    for i in range(count):
        a = random.randint(0,tough)
        b = random.randint(0,tough)
        suiji = random.randint(1,5)
        key = 1
        if suiji == 1:
            while True:
                answer = int(input("%d + %d = " % (a, b)))
                if answer == sum(a,b):
                    if key == 1:
                        print("恭喜您答对了!")
                        score += 1
                    break
                else:
                    key = 0
                    print("您的答案是错误的")
        if suiji == 2:
            while True:
                answer = int(input("%d - %d = " % (a, b)))
                if answer == sub(a,b):
                    if key == 1:
                        print("恭喜您答对了!")
                        score += 1
                    break
                else:
                    key = 0
                    print("您的答案是错误的")
        if suiji == 3:
            while True:
                answer = int(input("%d * %d = " % (a, b)))
                if answer == mul(a,b):
                    if key == 1:
                        print("恭喜您答对了!")
                        score += 1
                    break
                else:
                    key = 0
                    print("您的答案是错误的")
        if suiji == 4:
            if b == 0:
                continue
            while True:
                answer = eval(input("%d / %d = " % (a, b)))
                if answer == div(a,b):
                    if key == 1:
                        print("恭喜您答对了!")
                        score += 1
                    break
                else:
                    key = 0
                    print("您的答案是错误的")
    print("您的得分是: ",score)
    print("总分是: ",count_real)


print("=========================================================")
print("===================!!!感谢您的使用!!!===================")
print("=========================================================")


img

(2)计算器功能运行:

img

(3)随机加减乘除题目加计分

img

(4)上传到gittee

img

附上链接:https://gitee.com/ZHaoRanZ/calculator

四、实验过程中遇到的问题和解决过程

问题一:无法控制除法的位数
问题一解决方案:print(round(a/b,2))即可保留两位小数

问题二:除法的除数不能为0,出现报错
问题二解决方案:当除数为0时,提示并重新输入或进入下一个循环

问题三:当用户在进行除法运算训练时,会出现无法输入浮点数的问题
问题三解决方案:除法的answer = eval(input()),这样就可以输入浮点数了

五、参考资料

https://blog.csdn.net/m0_54235394/article/details/118465259

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

110

社区成员

发帖
与我相关
我的任务
社区描述
人生苦短,我用Python!
python3.11 高校
社区管理员
  • blackwall0321
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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