【问题标题】:TypeError : unsupported operand type(s) for +: 'NoneType' and 'NoneType'TypeError:+ 不支持的操作数类型:“NoneType”和“NoneType”
【发布时间】:2021-09-03 01:19:14
【问题描述】:

我是 python 的初学者,只是为了测试我的技能,试图制作一个程序来检查给定的整数是否可以表示为两个素数之和,我在下面提到的代码 -

a = int(input('Enter the number: '))
def prime(b):
    for i in range(2, b):
        while b % i == 0:
            pass
        else:
            return b
if a < 3:
    print("The number is too short to be tested")
else:
    for i in range(2, a):
        for j in range(2, a):
            d = prime(i)
            e = prime(j)
            if d + e == a:
                print("It can be expressed as a sum of prime numbers")
                break
    else:
        print("The number can not be expressed as a sum of prime numbers")

错误提示TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType' 我知道素数类需要先返回一个值,因此我将返回的值分别存储在变量 d 和 e 中,但徒劳无功。任何有关上述内容的帮助将不胜感激。

【问题讨论】:

  • 你希望你的函数prime()在给定参数的情况下返回什么?
  • 能否提供完整的错误回溯?
  • prime 应该只返回TrueFalse,您可以使用这些结果来决定是否要检查i + j == a

标签: python


【解决方案1】:

你的函数prime 做的事情太多了。只要它返回True,如果是素数,则返回False

def prime(b):
    for i in range(2, b):
        if b % i == 0:
            return False
    return True

您也不需要遍历两个范围:只需检查 ia - i 是否为素数(因为 i + a - i == a 根据定义)。此外,由于ia - i 围绕a/2 对称,因此您无需遍历从2 到a 的整个值范围。

a = int(input('Enter the number: '))

if a < 3:
    print("The number is too short to be tested")
else:
    for i in range(2, a // 2 + 1):
        if prime(i) and prime(a - i):
            print("It can be expressed as a sum of prime numbers")
            break
    else:
        print("The number can not be expressed as a sum of prime numbers")

作为练习,调整循环,以便您不必考虑 所有 介于 2 和 a// 2 + 1 之间的值,只需考虑 2 和奇数。

【讨论】:

    【解决方案2】:

    如果b % i == 0 语句从未在prime() 中统计,我建议您返回一个默认值。 另外请在此声明之前验证d和e是否确实存在(d + e) == a

    示例:(应该按原样运行)

    a = int(input('Enter the number: '))
    
    
    def prime(b, default=None):
        for i in range(2, b):
            if not b % i == 0:
                return b
        return default
    
    
    if a < 3:
        print("The number is too short to be tested")
    else:
        for i in range(2, a):
            for j in range(2, a):
                d, e = prime(i), prime(j)
                if d and e and (d + e) == a:
                    print("It can be expressed as a sum of prime numbers")
                    break
        print("The number can not be expressed as a sum of prime numbers")
    

    【讨论】:

      【解决方案3】:
      a = int(input('Enter the number: '))
      def prime(b):
          for i in range(2, b):
              if b % i == 0:
                  continue
              else:
                  return b
      if a < 3:
          print("The number is too short to be tested")
      else:
          for i in range(3, a + 1):
              for j in range(3, a + 1):
                  d = prime(i)
                  e = prime(j)
                  if d + e == a:
                      print("It can be expressed as a sum of prime numbers")
                      break
          else:
              print("The number can not be expressed as a sum of prime numbers")
      

      这种变化应该可以工作

      【讨论】:

      • 我已经尝试了你的代码,我得到了同样的错误报告。
      • 这是因为range(2, a) 在第一个循环中产生2prime(2)None。我已经确定了答案
      • 好的,但是现在当我输入5 时,我得到了否定的响应。我希望这样的程序知道2 + 3 = 5
      猜你喜欢
      • 1970-01-01
      • 2015-10-12
      • 1970-01-01
      • 2017-12-27
      • 2014-03-31
      • 2017-06-06
      • 2021-05-23
      • 2014-03-28
      • 2018-06-20
      相关资源
      最近更新 更多