python四则运算

论坛 期权论坛     
选择匿名的用户   2021-5-22 14:57   197   0
<div class="blogpost-body" id="cnblogs_post_body">
<h2>源代码已上传至码云,<a href="https://gitee.com/liudanxi/codes/njpay3luk8g1iv6bfs9t536">https://gitee.com/liudanxi/codes/njpay3luk8g1iv6bfs9t536</a></h2>
<h2>题目:</h2>
<p>  (1)能自动生成小学四则运算题目,并且不能出现负数;</p>
<p>  (2)能支持真分数的四则运算;</p>
<h2>思路:</h2>
<p>  (1)四则运算加减乘除,采用两个随机数,由于不能出现负数,则对两个随机数进行比较大小再进行减法运算,除法一向特殊,所以在随机数的取值范围中设置不包括0。</p>
<p>  (2)真分数运算在pycharm中导入fractions库,其余类似。</p>
<h2>实现过程:</h2>
<p>  设计三个函数:</p>
<p>    def newint() 生成整数四则运算</p>
<p>    def newfra() 生成真分数四则运算</p>
<p>    def newtest() 生成制定指定数量的四则运算题目</p>
<p>  函数关系:</p>
<p>    newint()与newfra()为独立的函数,负责生成随机四则运算,newtest()则随机调用上述两个函数生成题目。详细描述请看下方代码说明</p>
<h2>代码说明:</h2>
<p>  首先说明整数的四则运算,生成两个随机数并随机运算,在减法中比较大小防止出现负数,在除法中比较大小并循环取整除的随机数组合。最后输出算式并返回正确答案。</p>
<div class="cnblogs_code">
  <pre class="blockcode"><span style="color:#008080;"> 1</span> <span style="color:#0000ff;">def</span><span style="color:#000000;"> newint():
</span><span style="color:#008080;"> 2</span>     opr &#61; [<span style="color:#800000;">&#39;</span><span style="color:#800000;">+</span><span style="color:#800000;">&#39;</span>, <span style="color:#800000;">&#39;</span><span style="color:#800000;">-</span><span style="color:#800000;">&#39;</span>, <span style="color:#800000;">&#39;</span><span style="color:#800000;">×</span><span style="color:#800000;">&#39;</span>, <span style="color:#800000;">&#39;</span><span style="color:#800000;">÷</span><span style="color:#800000;">&#39;</span><span style="color:#000000;">]
</span><span style="color:#008080;"> 3</span>     fh &#61; random.randint(0, 3<span style="color:#000000;">)
</span><span style="color:#008080;"> 4</span>     n1 &#61; random.randint(1, 20<span style="color:#000000;">)
</span><span style="color:#008080;"> 5</span>     n2 &#61; random.randint(1, 20<span style="color:#000000;">)
</span><span style="color:#008080;"> 6</span>     rjg &#61;<span style="color:#000000;"> 0
</span><span style="color:#008080;"> 7</span>     <span style="color:#0000ff;">if</span> fh &#61;&#61;<span style="color:#000000;"> 0:
</span><span style="color:#008080;"> 8</span>         rjg &#61; n1 &#43;<span style="color:#000000;"> n2
</span><span style="color:#008080;"> 9</span>     <span style="color:#0000ff;">elif</span> fh &#61;&#61; 1<span style="color:#000000;">:
</span><span style="color:#008080;">10</span>         n1, n2 &#61;<span style="color:#000000;"> max(n1, n2), min(n1, n2)
</span><span style="color:#008080;">11</span>         rjg &#61; n1 -<span style="color:#000000;"> n2
</span><span style="color:#008080;">12</span>     <span style="color:#0000ff;">elif</span> fh &#61;&#61; 2<span style="color:#000000;">:
</span><span style="color:#008080;">13</span>         rjg &#61; n1 *<span style="color:#000000;"> n2
</span><span style="color:#008080;">14</span>     <span style="color:#0000ff;">elif</span> fh &#61;&#61; 3<span style="color:#000000;">:
</span><span style="color:#008080;">15</span>         n1, n2 &#61;<span style="color:#000000;"> max(n1, n2), min(n1, n2)
</span><span style="color:#008080;">16</span>         <span style="color:#0000ff;">while</span> n1 % n2 !&#61;<span style="color:#000000;"> 0:
</span><span style="color:#008080;">17</span>             n1 &#61; random.randint(1, 10<span style="color:#000000;">)
</span><span style="color:#008080;">18</span>             n2 &#61; random.randint(1, 10<span style="color:#000000;">)
</span><span style="color:#008080;">19</span>             n1, n2 &#61;<span style="color:#000000;"> max(n1, n2), min(n1, n2)
</span><span style="color:#008080;">20</span>         rjg &#61; int(n1 /<span style="color:#000000;"> n2)
</span><span style="color:#008080;">21</span>     <span style="color:#0000ff;">print</span>(n1, opr[fh], n2, <span style="color:#800000;">&#39;</span><span style="color:#800000;">&#61; </span><span style="color:#800000;">&#39;</span>, end&#61;<span style="color:#800000;">&#39;&#39;</span><span style="color:#000000;">)
</span><span style="color:#008080;">22</span>     <span style="color:#0000ff;">return</span> rjg</pre>
</div>
<p>真分数四则运算类似。</p>
<div class="cnblogs_code">
  <pre class="blockcode"><span style="color:#008080;"> 1</span> <span style="color:#0000ff;">def</span><span style="color:#000000;"> newfra():
</span><span style="color:#008080;"> 2</span>     opr &#61; [<span style="color:#800000;">&#39;</span><span style="color:#800000;">+</span><span style="color:#800000;">&#39;</span>, <span style="color:#800000;">&#39;</span><span style="color:#800000;">-</span><span
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP