编写python代码来计算几何级数

编写python代码来计算几何级数

本文介绍了编写python代码来计算几何级数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程和 Python 的新手.我需要帮助编写一个几何级数来计算级数 1,2,4,8,16...这是我目前所拥有的:

Im new to programming and python. I need help with coding a geometric progression thats supposed to calculate the progression 1,2,4,8,16... Heres what I have so far:

def work_calc (days_worked, n):
      temp=int(1)
      if days_worked<2:
            print (1)
      else:
            while temp <= days_worked:
                  pay1 = (temp**2)
                  pay = int(0)
                  pay += pay1
                  temp +=1
                  print ('your pay for ',temp-1,'is', pay1)

main()

现在它给了我这个输出:1, 4, 9, 16, 25我需要:1,2,4,8,16,32...

Right now it gives me this output: 1, 4, 9, 16, 25i need : 1,2,4,8,16,32...

我正在编写基本上应该这样做的代码:

im writing code that basically should do this:

Example:
Enter a number: 5
your value 1 is: 1
your value 2 is : 2
your value 3 is : 4
your value 4 is : 8
your value 5 is : 16
your total is: 31

在此先感谢您的帮助和指导!P.S:在编程方面,我有时(主要是)像一个愚蠢的金发女郎,所以感谢您的耐心..

Thanks in advance for your help and guidance!P.S: Im like a dumb blonde sometimes(mostly) when it comes to programming, so thanks for your patience..

推荐答案

我知道这对于您想要做的事情来说可能有点矫枉过正,并且已经在其他答案中就如何解决您的问题提供了很好的建议,但是介绍python的一些其他功能这里是一些其他方法:列表理解:

I understand this is probably overkill for what you are looking to do and you've been given great advice in the other answers in how to solve your problem but to introduce some other features of python here are some other approaches:List comprehension:

def work_calc(days):
    powers_of_two = [2**x for x in range(days)]
    for i, n in enumerate(powers_of_two):
        print('your pay for {} is {}'.format(i+1,n))
    print('your total is {}'.format(sum(powers_of_two)))

这是紧凑而整洁的,但会将整个 2^n 列表保存在内存中,对于小 n 这不是问题,但对于大的可能会很昂贵.生成器表达式与列表推导式非常相似,但将计算推迟到迭代结束.

This is compact and neat but would hold the whole list of 2^n in memory, for small n this is not a problem but for large could be expensive. Generator expressions are very similar to list comprehensions but defer calculation until iterated over.

def work_calc(days):
    powers_of_two = (2**x for x in range(days))
    total = 0
    for i, n in enumerate(powers_of_two):
        total += n
        print('your pay for {} is {}'.format(i+1,n))
    print('your total is {}'.format(total))

不得不将总数移动到滚动计算中并且每次仍然计算 2**n,生成器函数将避免功率计算:

Had to move the total to a rolling calculation and it still calculates 2**n each time, a generator function would avoid power calculation:

import itertools

def powers_of_two():
    n = 1
    while True:
        yield n
        n *= 2

def work_calc(days):
    total = 0
    for i, n in enumerate(itertools.islice(powers_of_two(), days)):
        total += n
        print('your pay for {} is {}'.format(i+1,n))
    print('your total is {}'.format(total))

正如我所说的矫枉过正,但希望能介绍一些 Python 的其他功能.

As I said overkill, but hopefully introduces some of the other features of python.

这篇关于编写python代码来计算几何级数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 04:09