简体   繁体   English

如何使用for循环编写一个迭代函数来计算前n个奇数的和,它返回1 + 3 +…+(2n-1)?

[英]How do I write an iterative function that computes the sum of the first n odd numbers it returns 1 + 3 + … + (2n - 1), using a for loop?

Assuming that n is a positive integer 假设n是一个正整数

this is my code: 这是我的代码:

def sum_odd_n(n):
    x =1
    for x in range(n):
        if x%2==1:
            continue
        return x + 2

but when I run it on Python it gives me the answer 2 . 但是当我在Python上运行它时,它给了我答案2 Could you help me by telling me what's wrong and what I should do to solve this? 您能否通过告诉我什么地方出了问题以及我应该怎么做才能解决这个问题?

Since you want to find the sum of first 'n' odd numbers, I suggest you to use range function with step=2. 由于您要查找第一个“ n”个奇数之和,因此建议您使用step = 2的范围函数。 I'll elaborate: 我会详细说明:

def sum_n(n):
    addition=0
    for x in range(1,2*n,2):
        addition+=x
    return addition
s=sum_n(5)
print(s)

This gives output as: 25 输出为: 25

Here, in range function, 1st attribute provides starting point, 2nd attribute provides the end point, and 3rd attribute gives the Difference between each number in the sequence. 在此,在范围函数中,第一个属性提供起点,第二个属性提供终点,第三个属性提供序列中每个数字之间的差。 I hope this helps. 我希望这有帮助。

There are a few problems with your code. 您的代码有一些问题。

  • The first is that you have a return statement inside the for loop. 首先是您在for循环中有一个return语句。
  • Secondly, you just visit the first n integers and check which of them are odd. 其次,您只需访问前n个整数,并检查其中哪些是奇数。 You won't visit all the first n odd integers. 您不会访问所有前n个奇数整数。

A list comprehension solution solution is as follows. 列表理解解决方案的解决方案如下。

 def sum_odd_n(n): # sum up the first n odd numbers return sum([2*i + 1 for i in range(n)]) 

Check this program it will work: 检查此程序是否可以运行:

a=int(input("how many first odd number sum you want"))

x=1

i=0

def OddSum():

      global i
      global x
      while i<=a:
            x+=2
            i+=1
      print(x)
OddSum()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写一个函数 function(n) 接受一个整数,并使用 while 循环返回前 n 个偶数的总和? - How do I write a function function(n) that takes in an integer, and returns the sum of the first n even numbers using a while loop? 如何编写递归 function 来计算对一组数字中的值求和的方法? - How do I write a recursive function that computes ways to sum to a value in a set of numbers? 前n个奇数的平方和 - sum of squares of the first n odd numbers 求前N个奇数之和的程序 - program to find the sum of the first N odd numbers 如何编写一个函数,它接受一个正整数 N 并返回前 N 个自然数的列表 - How to write a function that takes a positive integer N and returns a list of the first N natural numbers 我想根据用户输入 N 使用递归 function 计算前 N 个偶数的总和 - I want to compute the sum of first N even numbers based on the user input N using recursive function 前 n 个斐波那契数的和 - Sum of the first n fibonacci numbers python协方差矩阵返回2N,2N矩阵而不是N,N? - python covariance matrix returns a 2N,2N matrix instead of N,N? 如何使用递归 function 计算和打印以下序列的前 N 个元素的总和 - How can i calculate and print the sum of first N elements of below sequence using recursive function 如何编写一个 for 循环程序来查找范围内所有奇数的总和,但不使用 if function - How to write a for loop program that finds the sum of all odd numbers in a range, but does not use the if function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM