5 回答

TA貢獻1780條經驗 獲得超5個贊
produkt = 1
counter = 2
while produkt < 1000:
print(produkt, end=" ")
produkt = produkt * counter
counter += 1
print(produkt)
1 2 6 24 120 720 5040

TA貢獻1821條經驗 獲得超5個贊
只是為了好玩。
>>> next(filter(1000 .__lt__, itertools.accumulate(itertools.count(1), operator.mul))) 5040

TA貢獻1812條經驗 獲得超5個贊
我想你想要:
number = 1
produkt = number
while produkt<=1000:
print(produkt)
number += 1
produkt = produkt * number
結果:
1
2
6
24
120
720

TA貢獻1829條經驗 獲得超4個贊
您需要 2 個變量,一個用于存儲,total另一個用于存儲index(1, 2, 3, ...)
index = 1
total = 1
while index < 1000:
total *= index # same as: total = total * index
index += 1 # same as: index = index +1
# print(f'{index - 1} => {total}') uncomment if you want to follow the values
print(f"Stop at total {total}, index was {index}")
你會得到以下內容
1 => 1
2 => 2
3 => 6
4 => 24
5 => 120
6 => 720
7 => 5040
8 => 40320
9 => 362880
10 => 3628800
...
Stop at total 402...000, index was 1000

TA貢獻1752條經驗 獲得超4個贊
您可以按以下方式修改代碼:變量produkt取值 1,2,3,...,它們的乘積存儲在produkt_end.
produkt = 1
produkt_end = 1
while True:
print(produkt_end)
produkt_end = produkt_end*produkt
produkt = produkt+1
if produkt_end > 1000:
break
它將顯示produkt_end:
1
1
2
6
24
120
720
添加回答
舉報