7.1 函數(shù)input()的工作原理
讓程序暫停,等待客戶輸入文本,并存儲(chǔ)在變量中;
一般情況下,()中輸入提示文字,若提示超過(guò)一行:
prompt = 'blabla'
prompt += '\nblabla' #注意這里的語(yǔ)法和換行符
name = input(prompt)
7.1.1 數(shù)值輸入
age = input('how old are you?')
默認(rèn)輸入的是字符串,
age = int(age) #將變量用數(shù)值表示
age = str(age) #將變量用字符串表示
7.1.2 求模運(yùn)算符%
>>>10 % 3
1
>>>7 % 2
1
7.2 ?while循環(huán)
for循環(huán)針對(duì)集合的每個(gè)元素;
while循環(huán)不斷運(yùn)行,直到條件不滿足
7.2.1 使用while循環(huán)
current_number = 1
while current_number <= 120:
? ? print(current_number)
? ? current_number += 1 #表示current_number = current_number + 1
7.2.2 讓用戶選擇何時(shí)退出
prompt = '\nTell me something and I will repeat for you.'
prompt += "\nEnter 'quit' to end this program."
message = '' #讓python首次執(zhí)行while代碼時(shí)有可供檢查的東西
while message != 'quit':
? ? message = input(prompt)
? ? print(message)
7.2.3 使用標(biāo)志
定義一個(gè)變量,用于判斷整個(gè)程序是否處于活動(dòng)狀態(tài),這個(gè)變量稱為標(biāo)志
prompt = '\nTell me something and I will repeat for you.'
prompt += "\nEnter 'quit' to end this program."
active = True?
while active: #只要變量為T(mén)rue,循環(huán)就會(huì)執(zhí)行
? ? message = input(prompt)
? ? if message == 'quit':
? ? ? ? active = False
? ? else:
? ? ? ? print(message)
7.2.4 使用break退出循環(huán)
break用于控制程序流程,可使用它來(lái)控制哪些代碼將執(zhí)行
在任何python循環(huán)中都可使用break循環(huán),例如遍歷列表或字典的for循環(huán)
prompt = '\nPlease enter the city name you have visted.'
prompt += "\n Enter 'quit' when you are finished."
while True:
? ? city = input(prompt)
? ? if city == 'quit':
? ? ? ? break
? ? else:
? ? ? ? print("I'd like to visit " + city.title() + '!')
7.2.5 循環(huán)中使用continue
current_number = 0
while current_number < 10:
? ? current_number += 1
? ? if current_number % 2 == 0:
? ? ? ? continue #忽略下面的代碼,返回到循環(huán)的開(kāi)頭
? ? print(current_number)
7.2.6 避免無(wú)限循環(huán)
每個(gè)while循環(huán)都必須有停止運(yùn)行的途徑
7.3 使用while循環(huán)來(lái)處理字典和列表
for循環(huán)中不應(yīng)修改列表;while循環(huán)可以在遍歷同時(shí)對(duì)其進(jìn)行修改
7.3.1 在列表之間移動(dòng)元素
# -*- coding: utf-8 -*-
#分別創(chuàng)建一個(gè)待驗(yàn)證列表和已驗(yàn)證用戶的列表
unconfirmed_users = ['alice', 'einstein', 'damon', 'maria']
confirmed_users = []
#驗(yàn)證每個(gè)用戶,直到未驗(yàn)證客戶列表為空
#將每個(gè)驗(yàn)證的列表都移動(dòng)到已驗(yàn)證用戶列表中
while unconfirmed_users: #該列表不為空就會(huì)執(zhí)行循環(huán)
? ? current_user = unconfirmed_users.pop()
? ? confirmed_users.append(current_user)
print(confirmed_users)
print(unconfirmed_users) #輸出為空
7.3.2 刪除包含特定值的所有列表元素
pets = ['cat', 'monkey', 'cat', 'bull', 'tiger', 'rabbit', 'dragon']
while 'cat' in pets:
? ? pets.remove('cat')
print(pets)
7.3.3 使用用戶輸入來(lái)填充字典
# -*- coding: utf-8 -*-
vacation_land = {} #先定義一個(gè)空字典
active = True #設(shè)置標(biāo)志
while active:
? ? name = input('what is your name?')
? ? place = input('\where would you like to go?')
? ? vacation_land[name] = place
? ? repeat = input('is there anyone else to travel with?yes/ no')
? ? if repeat == 'no':
? ? ? ? active = False
print('\nThe search results is all below:')
for name, place in vacation_land.items():
print(name.title() + ' would like to visit ' + place.title() + #太長(zhǎng)換行
' during the holiday!')