提取函数
我写了一个简单的程序,可以计算出给定数量的完美平方。我的代码是:提取函数
"""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()
您的代码有很多错误!我想你使用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()
嗨,感谢您的回复,您的代码运行,但不是只打印完美的正方形,而是打印从2到'upper_bound'的每一个数字。 –
我不能相信你的完美广场有双重循环!所以你不能得到它!我已经更新了答案!你可以再试一次! –
另一个问题是,当我测试你的代码时使用了一个负数'upper_bound',它不打印'你必须输入一个正数.',你能解决这个问题吗? –
暗示:你错过了原来代码中的while循环'而UPPER_BOUND是无:' – balki
我本来是包括在内,但拿出来,看它是否固定的我得到的错误,但可悲的是,这不是问题所以现在我正在寻找另一种解决方案 –