初學Python(3)迴圈語句

十二分熱愛發表於2018-08-03

1While迴圈

n = 100
 
sum = 0
counter = 1
while counter <= n:
    sum = sum + counter
    counter += 1
 
print("1 到 %d 之和為: %d" % (n,sum))

While迴圈使用else語句

count = 0
while count < 5:
   print (count, " 小於 5")
   count = count + 1
else:
   print (count, " 大於或等於 5")

2for迴圈

例項

>>>languages = ["C", "C++", "Perl", "Python"] 
>>> for x in languages:
...     print (x)
... 
C
C++
Perl
Python
>>>

 

相關文章