與小卡特一起學python 第8章 轉圈圈 FOR迴圈和條件迴圈

yarking207發表於2016-04-01
#8章 轉圈圈
#8.1 一個非常簡單的FOR迴圈

for looper in [1,2,3,4,5]:
    print ("hello")
#8.2 每次for 迴圈做不同的事情
for looper in [1,2,3,4,5]:
    print (looper)
#8.3 計數迴圈 列印8的乘法表
for looper in [1,2,3,4,5]:
    print (looper,"times 8 =",looper * 8)
    #print (looper,"times 3 =",looper * 3,"times 4 =",looper * 4,"times 5 =",looper * 5)
#8.4  一條捷徑 range()
for looper in range(1,5):
    print (looper,"times 8 =",looper * 8)
#8.5 使用range()列印8的乘法表
for looper in range(1,11):
     print (looper,"times 8 =",looper * 8)
# 迴圈變數名 慣例 i,j,k
for i in range(1,5):
    print (i,"hello")
# range()簡寫 range(5) 等用range(0,5)
for i in range(5):
    print (i,"hello")
# 也可以用字串列表來迴圈
for letter in ("Hi there"):
    print(letter)
#傳遞形參
for i in range(1,10,2):
    print (i)
for i in range(2,10,2):
    print (i)
for i in range(5,26,5):
    print(i)
for i in range (10,0,-1):
    print(i)
#8.6 準備好了嗎?
import time
for i in range (10,0,-1):
    print(i)
    time.sleep(1)
print("Blast off!")
#8.7 誰最酷?
for cool_guy in ["Spongebob","Spiderman","Justin Timberlake","My Dad"]:
    print (cool_guy,"is the coolest guy ever!")
#8.8 條件或者while迴圈
print("Type 3 to contiue,anything else to quit.")
someInput = input()
while someInput == '3':
    print("Thank you for the 3.Very kind of you.")
    print("Type 3 to contiue,anything else to quit.")
    someInput = input()
print("That's not 3,so I'm quitting now.")
#8.9 迴圈中使用continue
for i in range (1,6):
    print()
    print("i=",i,)
    print("hello,how ",end='')  #python3 中end=''不換行。python3和python2的一些區別
    if i == 3:
        continue
    print("are you today?")
#8-9 break跳出迴圈,不再計數
for i in range (1,6):
    print()
    print("i=",i,)
    print("hello,how ",end='')  #python3 中end=''不換行。python3和python2的一些區別
    if i == 3:
        break
    print("are you today?")

    

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/220205/viewspace-2073984/,如需轉載,請註明出處,否則將追究法律責任。

相關文章