我給了一個任務來找出前n個奇數的平方和,其中n由用戶給出。前n個奇數的平方和
這個任務是;
The script only takes one input, so do not
# include any input command below. The print commands below are already given.
# Do not alter the print commands. Do not add any other prints commands.
r = range(1, n + 1) # prepare the range
result = 0 # initialise the result
for k in r: # iterate over the range
result = result + k # update the result
compute the sum of the squares of the first n odd numbers, and print it
這就是我迄今爲止所做的;
r = range(1, n ** n, 2)
result = 0
for k in r:
result = result + k
我知道的範圍內是錯誤的,因爲當我跑了,我用5正和我預想的答案是165,因爲前5個奇數的平方是165,而是我得到了144
請幫忙
您的代碼是沒有意義的。你沒有采用第一個「n」個奇數,或者將它們平方。也許你應該首先解決問題:第一步,你可以創建第一個「n」奇數的列表或迭代器嗎? – jonrsharpe
sum(k * k在範圍內(1,2 * n,2)) –
@babyblue這就是您需要的整個代碼(除了輸入'n'和打印) – DeepSpace