函式input()
input()
:暫停程式,等待使用者輸入文字並把文字儲存在指定變數中。並接受一個引數,即要向使用者顯示的提示或說明。int()
:使用input()
函式時,輸入的文字會被當成字串,所以可以用這個函式將其化為int型。%
:求模運算子。講兩個數相除並返回餘數。
下面舉個例子:
message = input("Tell me your name:")
print(message)
message = input("Tell me your years:")
if int(message) >= 18:
print('wow')
print(int(message) % 6)
#執行結果:
Tell me your name:mike
mike
Tell me your years:19
wow
1複製程式碼
while迴圈
迴圈簡介
while迴圈不斷執行,直到指定的條件不滿足位置。例如:
current_number = 1
while current_number <= 5:
print(current_number)
current_number += 1
#執行結果:
1
2
3
4
5複製程式碼
還可以讓使用者選擇何時退出,只要把迴圈的條件由current_number <= 5
改為message != 'quit'
,然後在迴圈語句裡面新增message = input(prompt)
,即:
current_number = 1
while message != 'quit':
message = input('Enter 'quit' to end the program or go on:')
print(current_number)
current_number += 1
#執行結果:
Enter 'quit' to end the program or go on:
1
Enter 'quit' to end the program or go on:
2
Enter 'quit' to end the program or go on:quit複製程式碼
這時候我們提示了一條訊息:要麼輸入quit來結束程式,要麼繼續執行。當然我們還可以設定標誌來決定迴圈的條件,比如利用布林值True
或者False
。
退出迴圈break
:不管條件測試結果如何,立即退出迴圈,不在執行迴圈中餘下程式碼。
prompt = "\nPlease enter a city you have visited:"
prompt += "\n(Enter 'quit' when you are finished.) "
while True:
city = input(prompt)
if city == 'quit':
break
#執行結果:
Please enter a city you have visited:
(Enter 'quit' when you are finished.) China
I'd love to go to China!
Please enter a city you have visited:
(Enter 'quit' when you are finished.) San Francisco
I'd love to go to San Francisco!
Please enter a city you have visited:
(Enter 'quit' when you are finished.) quit複製程式碼
continue
:跳出此次迴圈,即返回到迴圈開頭,並判斷迴圈條件是否滿足來決定是否執行迴圈。
current_number = 0
while current_number < 10:
current_number += 1
if current_number % 2 == 0:
continue
print(current_number)
#執行結果:
1
3
5
7
9複製程式碼
使用迴圈處理列表和字典
for迴圈中不應修改列表,因為for迴圈中修改列表之後,python的遍歷會出現問題。而要想遍歷列表並修改,可以用while迴圈。下面舉幾個例子。
在列表之間移動元素
#建立待驗證使用者列表和一個已驗證使用者列表
unconfirmed_ids = ['mike', 'lili', 'ace']
confirmed_ids = []
#驗證每個使用者並把其移動到已驗證使用者列表
while unconfirmed_ids:
current_id = unconfirmed_ids.pop()
print("Verifying id: " + current_id.title())
confirmed_ids.append(current_id)
#顯示所有已驗證id
print("\nThe following ids have been confirmed:")
for confirmed_id in confirmed_ids:
print(confirmed_id.title())
#執行結果:
Ace
Lili
Mike複製程式碼
刪除包含特定值的所有列表元素
nums = ['1', '3', '5', '1', '6', '1', '7']
print(nums)
while '1' in nums:
pets.remove('1')
print(nums)
#執行結果:
['3', '5', '6', '7']複製程式碼
使用使用者輸入來填充字典
responses = {}
#設定一個標誌Lj指出調查是否繼續
polling_active = True
while polling_active:
#提示輸入被調查者的名字和回答
name = input("\nWhat is your name? ")
response = input("Which mountain would you like to climb someday? ")
#將答卷儲存在字典中
responses[name] = response
#看看是否有人要參與調查
repeat = input("Would you like to let another person respond? (yes/ no) ")
if repeat == 'no':
polling_active = False
#調查結束顯示結果
print("\n--- Poll Results ---")
for name, response in responses.items():
print(name + " would like to climb " + response + ".")
#執行結果:
What is your name? Eric
Which mountain would you like to climb someday? Denali
Would you like to let another person respond? (yes/ no) yes
What is your name? Lynn
Which mountain would you like to climb someday? Devil's Thumb
Would you like to let another person respond? (yes/ no) no
--- Poll Results ---
Lynn would like to climb Devil's Thumb.
Eric would like to climb Denali.複製程式碼