def main():
for sayi in range(1,100):
if not sayi%2==0:
sonuc=hesapla(sayi)
print(sonuc)
def hesapla(sayi):
carpim = 1
if carpim == 1:
carpim=carpim*sayi
return carpim
main()
它没有用,我想要打印:
1
3
15
105
945
10395
135135
2027025
34459425
654729075
13749310575
316234143225
.................
但它正在打印:
1
3
5
7
9
11
13
15
17
19
21
...............
答案 0 :(得分:0)
您只能在一个功能中执行此操作。见下文。
def main():
sonuc = 1
for sayi in range(1,100):
if not sayi%2==0:
sonuc=sonuc*sayi
print(sonuc)
main()
这样做的两种功能方式如下:
def main():
sonuc=1
for sayi in range(1,100):
if not sayi%2==0:
sonuc=hesapla(sonuc,sayi)
print(sonuc)
def hesapla(sonuc,sayi):
carpim=sonuc*sayi
return carpim
main()
编辑以下是使用步骤的简化方法,而不是使用i%2检查:
def main():
sonuc=1
for sayi in range(1,100,2):
sonuc=hesapla(sonuc,sayi)
print(sonuc)
def hesapla(sonuc,sayi):
return sonuc*sayi
main()
答案 1 :(得分:0)
您需要保留正在运行的产品,而不是每次都重新计算它。
x = 1
for i in range(1, 100, 2):
x *= i
print(x)
编辑:
你真的不需要两个功能来做到这一点。这是浪费资源而且不必要的复杂。我会假设有人让你这样做
def main():
for i in range(1, 100, 2):
print(calculate(i))
def calculate(i):
x = 1
for n in range(1, i+1, 2):
x *= n
return x