四则运算

1.项目地址:https://github.com/Lizhizhu/size-/tree/master

2.PSP表格记录估计耗时和实际耗时:

PSP2.0

任务内容

计划完成需要的时间(min)

实际完成需要的时间(min)

Planning

计划

50

50 

 Estimate

估计这个任务需要多少时间,并规划大致工作步骤

20

20 

Analysis

需求分析 (包括学习新技术)

60

 50

Design

具体设计

50

 60

Coding

具体编码

400

 450

test

测试(自我测试,修改代码,提交修改)

200

300 

Postmortem & Process

Improvement Plan

事后总结 ,并提出过程改进计划

30

30 

Summary

合计

 

810

                960

3.题目描述:通过python语言编写一个能够自动生成小学四则运算的程序,同时,除了整数外,还要支持真分数的四则运算。

4.程序设计:

(1)基础功能:实现四则运算题目的自动生成,并打印出题目的答案

(2)扩展功能:实现菜单操作

5.实现代码:

复制代码
#简单的加减计算
import random
count=0
right=0
while True:
   a= random.randint(0,9)
   b= random.randint(0,9)
   print('%d + %d = ' %(a,b))
   question = input('请输入您的答案:(q退出)')
   result = a + b
   if question == str(result):
       print('回答正确')
       right += 1
       count += 1
   elif question == 'q':
       break
   else:
       print('回答错误')
       count += 1

percent = right / count
print('测试结束,共回答%d道题,正确个数为%d,正确率为%.2f%%' %(count,right,percent * 100))
复制代码

复制代码
#四则运算
import random
count=0
right=0
op = ['+','-','*','/']
while True:
   a=random.randint(0,99)
   b=random.randint(0,99)
   s=random.choice(op)
   print('%d %s %d'%(a,s,b))
   question=input('请输入你的答案:(q退出)')
   if s=='+':
       result=a + b
   elif s=='-':
       result=a - b
   elif s=='*':
       result=a * b
   elif s=='/':
       result=a / b
   if question == str(result):
         print('回答正确')
         right += 1
         count += 1
   elif question == 'q':
         break
   else:
         print('回答错误')
         count += 1
percent = right / count
print('测试结束,共回答%d道题,正确个数为%d,正确率为%.2f%%' %(count,right,percent * 100))
复制代码

原文地址:https://www.cnblogs.com/lzz807237221/p/13742677.html