亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我如何使用 while 循環和 Python 進行編碼

我如何使用 while 循環和 Python 進行編碼

DIEA 2023-08-08 09:57:59
創建一個程序,將 1、2、3... 中的所有數字相乘,并在乘積大于 1000 時結束。我嘗試了這段代碼,但它不起作用。produkt = 1while produkt<1000:    print(produkt)    produkt = produkt*(produkt +1)    if produkt < 1000:        break
查看完整描述

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


查看完整回答
反對 回復 2023-08-08
?
收到一只叮咚

TA貢獻1821條經驗 獲得超5個贊

只是為了好玩。

>>> next(filter(1000 .__lt__, itertools.accumulate(itertools.count(1), operator.mul)))
5040


查看完整回答
反對 回復 2023-08-08
?
慕雪6442864

TA貢獻1812條經驗 獲得超5個贊

我想你想要:


number = 1

produkt = number

while produkt<=1000:

    print(produkt)

    number += 1

    produkt = produkt * number

結果:


1

2

6

24

120

720



查看完整回答
反對 回復 2023-08-08
?
浮云間

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


查看完整回答
反對 回復 2023-08-08
?
溫溫醬

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


查看完整回答
反對 回復 2023-08-08
  • 5 回答
  • 0 關注
  • 207 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號