Python 如何创建10倍的几何进度计划
我想用Python3编写一个程序,其中包含一个myprice函数,该函数返回从1开始按几何顺序递增的X值。。我想要X的值,即值的个数为3,几何级数为10。。这样我的程序就可以打印(10100) 我该怎么做 提前谢谢。。南塔Python 如何创建10倍的几何进度计划,python,yield,Python,Yield,我想用Python3编写一个程序,其中包含一个myprice函数,该函数返回从1开始按几何顺序递增的X值。。我想要X的值,即值的个数为3,几何级数为10。。这样我的程序就可以打印(10100) 我该怎么做 提前谢谢。。南塔 def myprice(X,geometrical progress): i=0 i += 1 while i < X: i = yield i for i in my price(3,10): pr
def myprice(X,geometrical progress):
i=0
i += 1
while i < X:
i =
yield i
for i in my price(3,10):
print(i)
def myprice(X,几何进度):
i=0
i+=1
而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
def myprice(x,几何因子=10):
"""
几何级数的生成器。默认因子为10。
初始术语为“开始=1”;
参数:
x:int
要生成的术语数
几何因子:int
几何因子[默认值:10]
"""
开始=1
i=0#几何项计数器
而我
基于@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