任何人都可以协助完成这项任务吗?

时间:2016-09-27 03:31:32

标签: python

当正方形的总和1 ^ 2 + 2 ^ 2 + 3 ^ 2 + ...超过输入m时,写出一个while循环。打印小于m的最大总和以及总和中的术语数。

Example: If m = 18 then 
1^2 +2^2 + 3^2 = 1 + 4 + 9 = 14
1^2 +2^2 + 3^2 + 4^2 = 1 + 4 + 9 + 16 = 30

因此,对于m = 18,你应打印出3和14。

这是我到目前为止所做的事情,而且我现在基本完全迷失了:

def sum_printer():
    y = input("Please enter a maximum number: ")
    y = int(y)
    for result in range(y):
        while result + result ** 2 >= y:
            break
        else:
            print(str(result) + "^2 =", result ** 2, end=" ")


def sum_of_squares_result(m, n):
    return sum(result ** 2 for result in range(m, n))

sum_printer()

无法弄清楚我应该从哪里开始。计算平方和并不是问题,当平方和超过用户输入时,也不会破坏for循环。我无法弄清楚如何根据输入打印3和14。

1 个答案:

答案 0 :(得分:1)

您的代码中出现的问题是您检查result + result**2 resultrange(y)的每个数字是1 + 1**2 >= m。您基本上是在检查2 + 2**2 >= mdef sum_printer(): ceil = int(input("What is the maximum to not be exceeded? ")) total = 0 curr = 1 while total + curr**2 < ceil: total += curr**2 curr += 1 curr -= 1 print(curr, total) 等。我就是这样做的:

SELECT 
First([MyTable].Col1) AS Col1, 
First([MyTable].Col2) As Col2, 
First([MyTable].Col3) As Col3,
First([MyTable].Col4) As Col4,
First([MyTable].Col5) As Col5,
First([MyTable].Col6) AS Col6, 
[MyTable].Col7 INTO [MyTable2]
FROM [MyTable]
WHERE ((([MyTable].OtherCol1)='S') AND (([MyTable].OtherCol2) Is Null))
GROUP BY [MyTable].Col7
HAVING ((([MyTable].Col7) Is Not Null Or ([MyTable].Col7)<>' '));