2017-09-27 4 views
0

私は与えられた数までの完全な正方形を計算する簡単なプログラムを書いています。私のコードは次のとおりです。私はいくつかの機能にコードの私のブロックを抽出しようとしています関数を抽出する

"""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() 
+0

ヒント: ' – balki

+0

私はもともと含まれていることだったが、それは私がなっていたエラーを修正したが、悲しいことにそれは問題ではなかったかどうかを確認するためにそれを取った:あなたはUPPER_BOUNDがNoneながら'元のコードでは、whileループを逃しましただから私は別の解決策を探しています –

答えて

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() 
+0

こんにちは、お返事いただきありがとうございます。あなたのコードは実行されますが、完璧な四角形だけを印刷するのではなく、2から 'upper_bound'までのすべての数字が表示されます。 –

+0

私はあなたの完璧な広場が二重ループを持っているとは思えません!あなたはそれを手に入れることができません!私はすでに答えを更新しています!もう一度やり直すことができます! –

+0

もう一つの問題は、あなたのコードを負の 'upper_bound'でテストすると、'正の数を入力する必要があります。 'と表示されません。 –

関連する問題