提取函数

时间:2017-09-27 01:39:54

标签: python extraction

我编写了一个简单的程序,可以计算出给定数字的完美平方。我的代码是:

"""Print all the perfect squares from zero up to a given maximum."""

def main():
   """Even the main function needs a docstring to keep pylint happy"""
   upper_bound = None
   while upper_bound is None:
       line = input("Enter the upper bound: ")
       if line.isnumeric():
           upper_bound = int(line)
       else:
           print("You must enter a positive number.")

   squares = []
   for num in range(2, upper_bound + 1):
       for candidate in range(1, num):
           if candidate * candidate == num:
               squares.append(num)

   print("The perfect squares up to {} are: ".format(upper_bound))
   for square in squares:
       print(square, end=' ')
   print()

我正在尝试将我的代码块提取到几个函数中,我已经提出了我认为可能的解决方案,但遗憾的是我无法让它运行,因为它给了我一个错误unsupported operand type(s) for +: 'NoneType' and 'int'。我似乎无法找到这个问题的根源,我想知道我的解决方案是否只是坏的,如果是的话,那会更好一点?

我的尝试

   """Print all the perfect squares from zero up to a given maximum."""

  def read_bound():
   """Reads the upper bound from the standard input (keyboard).
      If the user enters something that is not a positive integer
      the function issues an error message and retries
      repeatedly"""


   line = input("Enter the upper bound: ")
   if line.isnumeric() and int(line) >= 0:
       upper_bound = int(line)
   else:
       print("You must enter a positive number.")



  def is_perfect_square(num):
   """Return true if and only if num is a perfect square"""
     for num in range( 2, upper_bound + 1):
         for candidate in range(1, num):
             if candidate * candidate == num:
                 return True



   def print_squares(upper_bound, squares):
   """Print a given list of all the squares up to a given upper bound"""


   print("The perfect squares up to {} are: ". format(upper_bound))
   for square in squares:
       print(square, end= ' ')



  def main():
   """Calling the functions"""
   upper_bound = read_bound()
   squares = []
   for num in range(2, upper_bound + 1):
       if is_perfect_square(num):
           squares.append(num)

   print_squares(upper_bound, squares)


  main()

1 个答案:

答案 0 :(得分:0)

您的代码有很多错误!我猜你用的是python 3.x!我用python 2.7重写了一些代码!只需发布以下代码!如果你不能运行它,请告诉我!

"""Print all the perfect squares from zero up to a given maximum."""
def read_bound():
    """Reads the upper bound from the standard input (keyboard).
       If the user enters something that is not a positive integer
       the function issues an error message and retries
       repeatedly"""

    while True:
      try:
          upper_bound = int(input("Enter the upper bound: "))
      except Exception:
          print("You must enter a positive number.")
      else:
          return upper_bound


def is_perfect_square(num):
    """Return true if and only if num is a perfect square"""
    for candidate in range(1, num):
        if candidate * candidate == num:
            return True
    return False



def print_squares(upper_bound, squares):
    """Print a given list of all the squares up to a given upper bound"""
    print("The perfect squares up to {} are: ". format(upper_bound))
    for square in squares:
        print square, ' '


def main():
   """Calling the functions"""
   upper_bound = read_bound()
   squares = []
   for num in range(2, upper_bound + 1):
        if is_perfect_square(num):
           squares.append(num)

   print_squares(upper_bound, squares)

if __name__ == '__main__':
    main()