Python实现简单的四则运算
GitHub 项目地址
https://github.com/745421831/-/tree/master
PSP
|
PSP2.1 |
Personal Software Process Stages |
预估耗时(分钟) |
实际耗时(分钟) |
|
Planning |
计划 |
10 |
20 |
|
· Estimate |
· 估计这个任务需要多少时间 |
10 |
10 |
|
Development |
开发 |
360 |
600 |
|
· Analysis |
· 需求分析 (包括学习新技术) |
30 |
40 |
|
· Design Spec |
· 生成设计文档 |
30 |
40 |
|
· Design Review |
· 设计复审 (和同事审核设计文档) |
10 |
15 |
|
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
5 |
5 |
|
· Design |
· 具体设计 |
40 |
80 |
|
· Coding |
· 具体编码 |
300 |
500 |
|
· Code Review |
· 代码复审 |
60 |
120 |
|
· Test |
· 测试(自我测试,修改代码,提交修改) |
180 |
120 |
|
Reporting |
报告 |
120 |
60 |
|
· Test Report |
· 测试报告+博客 |
120 |
120 |
|
· Size Measurement |
· 计算工作量 |
10 |
10 |
|
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
40 |
30 |
|
合计 |
1325 |
1770 |
项目要求
- 能自动生成小学四则运算题目
- 除了整数以外,还要支持真分数的四则运算
解题思路
- 了解四则运算的基本法则
- 利用随机函数随机生成数字以及运算符
- 用户输入答案程序需要判断答案是否正确
- 支持真分数运算
设计实现及代码说明
import random
from fractions import Fraction
operation = ['+', '-', '*', '/'] #四则运算的符号
global f
def integer_score():
#rand = operation[random.randint(0,3)]
number = random.randint(1,4) #随机产生的表达式长度
f = ''
for i in range(number):
a = random.randint(1,20) #随机产生的表达式中的数
rand = operation[random.randint(0, 3)] #随机选择一个四则运算中的符号
if rand == '/':
b = random.randint(a, 20) #随机产生的真分数的分母
f += str(a) + rand + str(b) #数与符号相连
rand = operation[random.randint(0, 2)] #随机选择一个四则运算中的符号
f += rand
else:
f += str(a) + rand
#print(a,rand,end='')
b = random.randint(1, 20)
f += str(b) #得到完整的表达式
n = eval(f) #得到表达式的结果
n = Fraction('{}'.format(n)).limit_denominator() #小数转化为分数
if n > 0:
print('题目:')
print(f,'=')
print('请输出答案:')
x = Fraction('{}'.format(eval(input()))).limit_denominator()
if n == x: #输入的数与表达式比较
print(True)
else:
print(False)
print('正确的答案为:',n)
else:
integer_score()
def integer():
# rand = operation[random.randint(0,3)]
number = random.randint(1, 3)
f = ''
for i in range(number):
a = random.randint(1, 10)
rand = operation[random.randint(0, 3)]
f += str(a) + rand
b = random.randint(1, 10)
f += str(b)
n = eval(f)
if isinstance(n, int) and n > 0:
print('题目:')
print(f, '=')
print('请输出答案:')
x = eval(input())
if n == x:
print(True)
else:
print(False)
print('正确的答案为:', n)
else:
integer()
def score():
op = ['+', '-']
number = random.randint(1, 3)
f = ''
for i in range(number):
a = random.randint(1, 10)
b = random.randint(a, 10)
rand = op[random.randint(0, 1)]
f += str(a) + '/'+ str(b)+rand
a = random.randint(1, 10)
b = random.randint(a, 10)
f += str(a) + '/'+ str(b)
n = eval(f)
n = Fraction('{}'.format(n)).limit_denominator()
if n > 0:
print('题目:')
print(f,'=')
print('请输出答案:')
x = Fraction('{}'.format(eval(input()))).limit_denominator()
if n == x:
print(True)
else:
print(False)
print('正确的答案为:',n)
else:
score()
if __name__ == '__main__':
while True:
print('选择你想做的题目:')
print('0(退出)1(分数题目),2(整数题目),3(综合题目)')
m = int(input())
if m == 1:
score()
elif m == 2:
integer()
elif m == 3:
integer_score()
elif m == 0:
exit()
else:
print('请重新输入你的选择')
#isinstance(1, int)
总结
这次利用Python实现简单的四则运算还不够完美,比如没有随机生成带括号的由于时间原因以及个人能力有限,这些只能以后慢慢完善。
Python实现简单的四则运算的更多相关文章
- Python 实现简单的 Web
简单的学了下Python, 然后用Python实现简单的Web. 因为正在学习计算机网络,所以通过编程来加强自己对于Http协议和Web服务器的理解,也理解下如何实现Web服务请求.响应.错误处理以及 ...
- 利用ANTLR4实现一个简单的四则运算计算器
利用ANTLR4实现一个简单的四则运算计算器 ANTLR4介绍 ANTLR能够自动地帮助你完成词法分析和语法分析的工作, 免去了手写去写词法分析器和语法分析器的麻烦 它是基于LL(k)的, 以递归下降 ...
- 用 python实现简单EXCEL数据统计
任务: 用python时间简单的统计任务-统计男性和女性分别有多少人. 用到的物料:xlrd 它的作用-读取excel表数据 代码: import xlrd workbook = xlrd.open_ ...
- python开启简单webserver
python开启简单webserver linux下面使用 python -m SimpleHTTPServer 8000 windows下面使用上面的命令会报错,Python.Exe: No Mod ...
- Python开发简单爬虫 - 慕课网
课程链接:Python开发简单爬虫 环境搭建: Eclipse+PyDev配置搭建Python开发环境 Python入门基础教程 用Eclipse编写Python程序 课程目录 第1章 课程介绍 ...
- python使用简单http协议来传送文件
python使用简单http协议来传送文件!在ubuntu环境下,局域网内可以使用nc来传送文件,也可以使用基于Http协议的方式来下载文件我们可以使用python -m SimpleHTTPServ ...
- Python超简单的HTTP服务器
Python超简单的HTTP服务器 安装了python就可以 python -m SimpleHTTPServer 执行这一个命令即可实现一个HTTP服务器,将当前目录设为HTTP服务目录,可以通过h ...
- 教学项目之-通过Python实现简单的计算器
教学项目之-通过Python实现简单的计算器 计算器开发需求 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/ ...
- python多线程简单例子
python多线程简单例子 作者:vpoet mail:vpoet_sir@163.com import thread def childthread(threadid): print "I ...
随机推荐
- 小白のjava实现wc.exe功能
GitHub地址 项目完成情况 基本功能列表(已实现) wc.exe -c file.c //返回文件 file.c 的字符数 wc.exe -w file.c //返回文件 file. ...
- Echarts 柱状图配置详解
1.基本柱状图 // 指定图表的配置项和数据 var option = { // ---- 标题 ----- title: { text: '主标题', textStyle: { color: 're ...
- centos 时钟配置
centos 7 时钟配置: timedatectl 命令: [root@localhost ~]# timedatectl --help timedatectl [OPTIONS...] COMMA ...
- spring整合dubbo[单机版]
Spring整合Dubbo,这个是用xml配置的 (方式一) 来梳理下步骤: 1. 安装zookeeper,在进行简单配置[这里使用单机模式,不用集群] 2. 创建maven项目,构建项目结构 3. ...
- linux服务基础(三)之Httpd2.4配置
httpd-2.4 新特性: . MPM支持运行DSO机制,以模块形式按需加载 . 支持event MPM . 支持异步读写 . 支持每模块及每个目录分别使用各自的日志级别 . 每请求配置 <I ...
- vscode打开django项目pylint提示has not "object" member
vscode 打开 django 项目提示 has not "object" member 是因为 Django 动态地将属性添加到所有模型类中,所以 ide 无法解析. 解决方案 ...
- P4233 射命丸文的笔记
思路 题目要求求的是哈密顿回路的期望数量,实际上就是哈密顿回路的总数/有哈密顿回路的竞赛图的数量 n个点的所有竞赛图中哈密顿回路的总数为 \[ (n-1)! 2^{\frac{n(n-1)}{2}-n ...
- (转载)Unity3D所要知道的基础知识体系大纲,可以对照着学习,不定期更新
本文献给,想踏入3D游戏客户端开发的初学者. 毕业2年,去年开始9月开始转作手机游戏开发,从那时开始到现在一共面的游戏公司12家,其中知名的包括搜狐畅游.掌趣科技.蓝港在线.玩蟹科技.天神互动.乐元素 ...
- Docker Swarm Mode 学习笔记(聊聊 replicas)
在 Swarm 集群中, 创建服务时可以通过设置 --replicas 参数来指定此服务在工作节点上运行的任务数. 示例 这里我们来创建一个 nginx 服务作为示例: version: '3' se ...
- Python链接Oracle数据库
说明:以下所需安装的所有软件版本必须跟系统一致,即系统是64位,软件就得是64位,否则会出现各种链接报错的情况. 现以64位系统,python 3.6.5 64位为例: (一)安装cx_Oracle ...