關卡鏈接:
https://codecombat.163.com/play/level/fire-dancing編程
用循環節省輸入並拯救英雄!oop
簡介:
左 右 起舞,避開火球。學習
使用 while true 循環 重複代碼,像這樣:spa
while True:
hero.moveLeft()
# 這會反覆執行。
默認代碼
# 代碼一般按編寫順序執行。
# 循環會屢次重複一個代碼塊。
# 按Tab或4個空格,把移動指令縮進到循環內部。
while True:
hero.moveRight()
# 在這裏給循環里加 moveLeft 命令。
概覽
代碼一般按編寫順序執行。 循環 (loop) 讓你不用從新輸入一次就能執行不少次。3d
怎麼用 while-true 循環
咱們從 while 關鍵字 (keyword) 開始吧。它說的是 當 (while) 什麼是真的 (true) 時,就重複循環 循環體 (body) 裏的內容 。code
如今咱們要讓循環永遠執行,因此咱們用 while-true 循環 。真 (true) 的假不了!blog
先別太介意那個 true,咱們之後會詳細學習。只要知道 while-true 循環 是個一直重複的循環就行。ci
下面你會學習如何寫 while-true 循環:get
# 以 "while True:" 開頭
# 後面帶有縮進(前面四個空格)的語句都會被當成循環內容。
while True:
hero.moveRight()
hero.moveLeft()
hero.say("這行不在循環裏!")
焰中舞動 解法
# 代碼一般按編寫順序執行。
# 循環會屢次重複一個代碼塊。
# 按Tab或4個空格,把移動指令縮進到循環內部。
while True:
hero.moveRight()
# 在這裏給循環里加 moveLeft 命令。
hero.moveLeft()