TL! 2020-01-15 07:47 采纳率: 100%
浏览 366
已采纳

python3计算鸡兔同笼的程序,但会出现小数,怎么样修改能让它如果算出小数就print("此题无正确答案")?

def 鸡的只数(总只数,总脚数):
      return (4*总只数-总脚数)//2
def 兔的只数(总只数,总脚数):
      return (总脚数-2*总只数)//2
总只数= int(input("请输入鸡和兔共有几个头:"))
总脚数= int(input("请输入鸡和兔共有几只脚:"))
print("鸡的只数=",鸡的只数(总只数,总脚数))
print("兔的只数=",兔的只数(总只数,总脚数))
  • 写回答

1条回答 默认 最新

  • 7*24 工作者 2020-01-15 08:56
    关注
    # -*- coding:utf-8 -*-
    import sys
    
    def chickenNum(totals, foots):
        if (4 * totals - foots) % 2 == 1 or (foots - 2 * totals) % 2 == 1:
            sys.exit('此题无正确答案')
        return (4 * totals - foots) // 2
    
    
    def rabbitNum(totals, foots):
        if  (4 * totals - foots) % 2 == 1 or (foots - 2 * totals) % 2 == 1:
            sys.exit('此题无正确答案')
        return (foots - 2 * totals) // 2
    
    totals = int(input("请输入鸡和兔共有几个头:"))
    foots = int(input("请输入鸡和兔共有几只脚:"))
    print("鸡的只数=", chickenNum(totals, foots))
    print("兔的只数=", rabbitNum(totals, foots))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?