-1
寫一個while循環,當平方和1^2 + 2^2 + 3^2 + ...超過輸入m時退出。打印小於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
因此,你應該打印出3和14 M = 18
這是我到目前爲止,和我基本上完全在這一點上失去了:
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。
謝謝!似乎我沒有必要讓它變得太複雜了。 – Mike