110
社区成员
发帖
与我相关
我的任务
分享课程:《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("=========================================================")

(2)计算器功能运行:

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

(4)上传到gittee

附上链接:https://gitee.com/ZHaoRanZ/calculator
问题一:无法控制除法的位数
问题一解决方案:print(round(a/b,2))即可保留两位小数
问题二:除法的除数不能为0,出现报错
问题二解决方案:当除数为0时,提示并重新输入或进入下一个循环
问题三:当用户在进行除法运算训练时,会出现无法输入浮点数的问题
问题三解决方案:除法的answer = eval(input()),这样就可以输入浮点数了