我只需要爲每個if語句中的每個數字加上+30。我需要其中的36個,有沒有辦法讓烏龜如果陳述或類似的東西做得更多?我真的被卡住了,手動的方式會很瘋狂。有沒有辦法讓Python生成多個if語句?
例如:循環
if 0 <= x <=30 and 0 <= y <= 30:
turtle.drawsstuff
if 30 <= x <=60 and 0 <= y <= 60:
etc.
我只需要爲每個if語句中的每個數字加上+30。我需要其中的36個,有沒有辦法讓烏龜如果陳述或類似的東西做得更多?我真的被卡住了,手動的方式會很瘋狂。有沒有辦法讓Python生成多個if語句?
例如:循環
if 0 <= x <=30 and 0 <= y <= 30:
turtle.drawsstuff
if 30 <= x <=60 and 0 <= y <= 60:
etc.
使用。
for n in range(0, 36 * 30, 30):
if n <= x <= n + 30 and 0 <= y <= n + 30:
pass #do something
for n in range(0, 36 * 30, 30):
if n <= x <= (n+30) and n <= y <= (n+30):
pass # (do stuff)
range
可以採取爲 「步驟」 值的可選的第三個參數。作爲參考,請參閱Python's documentation on range
。
@AlexMartelli哎呀,你是對的。固定。 – Doorknob
這取決於你的if語句 –
if語句中的內容是什麼? – arshajii
海龜填滿了廣場 – user2848418