Python 2.6 非程式設計師教程/計數到 10
在這裡,我們介紹第一個控制結構。通常,計算機從第一行開始,然後向下執行。然而,控制結構改變了語句執行的順序,或者決定是否執行某些語句。以下是一個使用while控制結構的程式的原始碼
a = 0
while a < 10:
a = a + 1
print (a)
以下是非常令人興奮的輸出
1 2 3 4 5 6 7 8 9 10
你以為把你的電腦變成一個五美元的計算器後,情況不會更糟嗎?
那麼這個程式做了什麼呢?首先,它看到了a = 0這行程式碼,它告訴計算機將a設定為零。然後,它看到了while a < 10:,它告訴計算機檢查是否a < 10。計算機第一次看到這個 while 語句時,a等於零,這意味著a小於 10,所以計算機繼續執行接下來的縮排或製表符中的語句。執行完這個 while “迴圈”中的最後一條語句print (a)後,計算機又回到while a < 10檢查a的當前值。換句話說,只要a小於十,計算機就會執行縮排的語句。由於a = a + 1不斷地將 1 加到a上,最終 while 迴圈會使a等於十,並使a < 10不再成立。達到這一點後,程式將不再執行縮排的行。
請始終記住在“while”語句後新增冒號“:”!
以下是while的另一個使用示例
a = 1
s = 0
print ('Enter Numbers to add to the sum.')
print ('Enter 0 to quit.')
while a != 0:
print 'Current Sum:', s
a = input('Number? ')
s = s + a
print 'Total Sum =', round(s, 2)
Enter Numbers to add to the sum. Enter 0 to quit. Current Sum: 0 Number? 200 Current Sum: 200 Number? -15.25 Current Sum: 184.75 Number? -151.85 Current Sum: 32.9 Number? 10.00 Current Sum: 42.9 Number? 0 Total Sum = 42.9
注意print 'Total Sum =', s是如何只在最後執行的。while語句隻影響縮排的空白行。!=表示“不等於”,所以"while a != 0:"表示:“直到a為零,執行其後的縮排語句。”
現在我們有了 while 迴圈,就可以編寫永遠執行的程式了。一個簡單的實現方法是編寫這樣的程式
while 1 == 1:
print "Help, I'm stuck in a loop."
“==”運算子用於測試兩邊表示式是否相等,就像之前使用“<”表示“小於”一樣(在下一章中,您將獲得所有比較運算子的完整列表)。
這個程式會一直輸出Help, I'm stuck in a loop.,直到宇宙熱寂或您停止它,因為 1 將永遠等於 1。停止它的方法是同時按下 Control(或Ctrl)鍵和C(字母)。這將終止程式。(注意:有時您需要在按下 Control-C 後按 Enter)。
Fibonacci.py
# This program calculates the Fibonacci sequence
a = 0
b = 1
count = 0
max_count = 20
while count < max_count:
count = count + 1
# we need to keep track of a since we change it
old_a = a
old_b = b
a = old_b
b = old_a + old_b
# Notice that the , at the end of a print statement keeps it
# from switching to a new line
print(old_a),
輸出
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
注意透過在print語句末尾使用逗號來實現單行輸出。
Password.py
# Waits until a password has been entered. Use Control-C to break out without
# the password
# Note that this must not be the password so that the
# while loop runs at least once.
password = "no password"
# note that != means not equal
while password != "unicorn":
password = raw_input("Password: ")
print "Welcome in"
示例執行
Password: auo Password: y22 Password: password Password: open sesame Password: unicorn Welcome in
編寫一個程式,要求使用者輸入登入名和密碼。然後,當他們輸入“lock”時,需要輸入他們的姓名和密碼來解鎖程式。
編寫一個程式,要求使用者輸入登入名和密碼。然後,當他們輸入“lock”時,需要輸入他們的姓名和密碼來解鎖程式。
name = raw_input("What is your UserName: ")
password = raw_input("What is your Password: ")
print "To lock your computer type lock."
command = ""
input1 = ""
input2 = ""
while command != "lock":
command = raw_input("What is your command: ")
while input1 != name:
input1 = raw_input("What is your username: ")
while input2 != password:
input2 = raw_input("What is your password: ")
print "Welcome back to your system!"
如果您希望程式連續執行,只需在整個程式周圍新增一個while 1 == 1:迴圈。在程式碼開頭新增它時,您需要縮排程式的其餘部分,但不要擔心,您不必為每一行手動執行此操作!只需突出顯示您要縮排的所有內容,然後點選 python 視窗頂部欄“格式”下的“縮排”。請注意,您可以使用這樣的空字串:""。
另一種方法是
name = raw_input('Set name: ')
password = raw_input('Set password: ')
while 1 == 1:
nameguess=passwordguess=key="" # multiple assignment
while (nameguess != name) or (passwordguess != password):
nameguess = raw_input('Name? ')
passwordguess = raw_input('Password? ')
print "Welcome,", name, ". Type lock to lock."
while key != "lock":
key = raw_input("")
注意while中的or (name != "user") or (password != "pass"):,我們還沒有介紹它。您可能可以弄清楚它的工作原理。
login = "john"
password = "tucker"
logged=2
while logged != 0:
while login != "Phil":
login = raw_input("Login : ")
while password != "McChicken":
password = raw_input("Password: ")
logged = 1
print "Welcome!"
print "To leave type lock "
while logged == 1:
leave = raw_input (">> ")
if leave == "lock":
logged = 0
print "Goodbye!!"
雖然這種方法比較粗略,但它也有效。注意它使用了尚未介紹的if函式。