我怎样才能创建一个 10 的倍数的几何级数程序

how can i create a geometrical progress program multiple of 10

我想在 Python 3 中编写一个程序,其中包含一个 myprice 函数,该函数 returns X 值从 1 开始按几何级数递增。我想要 X 的值是值为 3,几何级数为 10.. 这样我的程序将打印 (1,10,100).

我该怎么做?

提前致谢..南蒂亚

def myprice(X,geometrical progress):
    i=0
    i += 1 
    while i < X:
        i =

        yield i

for i in my price(3,10):
    print(i)

@techUser。你可以这样写:

def myprice(x, geometrical_factor=10):
    """
    A generator of a geometrical progression. The default factor is 10.

    The initial term is 'start = 1';

    Parameter:
    x : int
      number of terms to generate
    geometrical_factor: int
      geometrical factor [default: 10]
    """
    start = 1

    i = 0 # Geometrical term counter
    while i < xterm:
        if i == 0:
            yield start
        else:
            start = start * geometrical_factor
            yield start
        i += 1

基于@eapetcho 的解决方案,

def myprice(x, fctr=10):
    """A generator of a geometrical progression. The default factor is 10.

    The initial term is 1.

    Args:
        x   (int): number of terms to generate
        ftr (int): geometrical factor. Default is 10

    """

    start = 1

    i = 0
    while i < x:
        if i == 0:
            yield start
        else:
            start = start * fctr
            yield start
        i += 1

for n in myprice(20, 2):
  print(n)

输出

1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288