Python之字典及while循環(huán)解讀
1.字典涉及的知識(shí)點(diǎn)
1.1 遍歷字典所有的鍵-值對
要點(diǎn):用item()方法
favorite_languages ={
'jen':'python'
'sarch':'C'
'enward':'ruby'
'phil':'python'
}
for name, language in favorite_languages.item():
print(name.title() + "'s favorite language is " + languages.title() + ".")這里的name,以及l(fā)anguage,都是變量,不用非要命名為key,value。
這樣命名可以方便用戶理解。
同時(shí)item()方法返回一個(gè)鍵-值對列表。
1.2 按順序遍歷字典所有的鍵
要點(diǎn):按順序用sorted()函數(shù),用for語句遍歷整個(gè)字典
favorite_languages ={
'jen':'python'
'sarch':'C'
'enward':'ruby'
'phil':'python'
}
for name in sorted(favorite_languages.keys()):
print(name.titile() + ",thank you for taking the poll.")這里的for語句類似于其他for語句,但對方法dictionary.keys()的結(jié)果調(diào)用了函數(shù)sorted()。
這使得Python列出了字典中的所有鍵,并在遍歷前對這個(gè)列表進(jìn)行排序。
1.3 考慮字典內(nèi)元素是否重復(fù)
要點(diǎn):set()集合類似與列表,確保每個(gè)元素獨(dú)一無二。
favorite_languages ={
'jen':'python'
'sarch':'C'
'enward':'ruby'
'phil':'python'
}
print("The following languages have been metionted.")
for language in set(favorite_languages.value()):
print(languages.title())1.4 嵌套字典列表
要求
1.生成30個(gè)外星人;
2.每個(gè)外星人包含顏色、生命值、和速度,并輸出;
3.前3個(gè)外星人的中,顏色為綠色的外星人,將其顏色更改為黃色;
4.并將對應(yīng)這些外星人的速度改為中速,生命值改為10。
//創(chuàng)建一個(gè)用于存儲(chǔ)外星人的空列表
aliens = []
//創(chuàng)建30個(gè)綠色的外星人
for alien_number in range(30):
new_alien = ['color' = 'green','point' = 5, 'speed' = 'slow']
aliens.append(new_alien)
for aliens in range[:3]:
if alien['color' = 'green']:
alien['color'] = 'yellow'
alien['point'] = 10
alien['speed'] = 'medium'
//顯示前五個(gè)外星人
for alien in aliens[:5]:
print(alien)
print("...") 1.5 在字典中存儲(chǔ)列表
//存儲(chǔ)所點(diǎn)披薩的信息
pizza = {
'crust': 'thick',
'toppings' = ['mushrooms','extra cheese'],
}
//概述所點(diǎn)的披薩
print("You order a " + pizza['crust'] + "-crust pizza " + "with the following toppings: ")
for topping in pizza['toppings']:
print("\t" + topping) 每當(dāng)一個(gè)鍵需要關(guān)聯(lián)多個(gè)值時(shí),就可以在字典中嵌套一個(gè)列表。在字典中存儲(chǔ)字典也是同理。
2.用戶輸入
2.1 input工作原理
input()讓程序暫停運(yùn)行,等待用戶輸入一些文本,當(dāng)用戶按回車鍵后繼續(xù)運(yùn)行。
輸入的變量存儲(chǔ)在message中,當(dāng)print時(shí)將值呈現(xiàn)給用戶。
prompt = "If you tell us who you are ,we can personalize the messages you see." prompt += "\nWhat is your first name? "
這是創(chuàng)建多行字符串的格式,運(yùn)算符 += 在存儲(chǔ)在prompt中的字符串在末尾加一個(gè)字符串。
2.2 使用int()來獲取數(shù)值輸入
age = input("How old are you?")
print('age')'19'
會(huì)看到輸出的age的數(shù)字帶有單引號(hào),屬于字符串類型。
因此當(dāng)此值與其他數(shù)值做比較的時(shí)候就會(huì)發(fā)生錯(cuò)誤。
因此使用 age = int(age)
age = input("How old are you?")
age = int(age)
age >= 18
True2.3求模運(yùn)算符
判斷數(shù)字是奇是偶
number = input(" Enter a number ,and I'll tell you if it's even or odd:")
number = int(number)
if number % 2 == 0:
print("\nThe number " + str(number) + " is even. ")
else:
print("\nThe number " + str(number) + " is odd. ")3. while循環(huán)
3.1 while 和 if 混合使用
for循環(huán)用于針對集合中的每個(gè)元素的一個(gè)代碼塊,而while循環(huán)則不斷運(yùn)行,直到指定的條件不滿足為止。
eg: 判斷是否退出游戲
prompt = "\nTell me something, and I Will repect it back you:"
prompt += "\nEnter 'quit' to end the program. "
message = " "
if message != 'quit':
message = input(prompt)
print(message) 但是此方法的缺點(diǎn)是 會(huì)將quit也打印出來。
因此 改進(jìn):
prompt = "\nTell me something, and I Will repect it back you:"
prompt += "\nEnter 'quit' to end the program. "
message = " "
while message != 'quit':
message = input(prompt)
if message != 'quit':
print(message) 也可使用標(biāo)志,這里我們名命此標(biāo)志為acitve(可指定任何名字),來判斷游戲是否運(yùn)行:
prompt = "\nTell me something, and I Will repect it back you:"
prompt += "\nEnter 'quit' to end the program. "
active = True
while avtive:
message = input(prompt)
if message == 'quit':
active = Flase
else:
print(message) 此結(jié)果與上一個(gè)方法的結(jié)果相同。
3.2 使用break退出循環(huán)
prompt = "\nTell me something, and I Will repect it back you:"
prompt += "\nEnter 'quit' to end the program. "
while True:
message = input(prompt)
if message == 'quit':
break
else:
print(message) 3.3 使用continue
要返回循環(huán)的開頭,并根據(jù)條件測試結(jié)果決定是否繼續(xù)執(zhí)行循環(huán),可以使用continue語句,它不像break語句那樣不再執(zhí)行余下的代碼并退出。
current_number = 0
while current_number < 10:
current_number += 1
if current_number % 2 ==0:
continue
print(current_number)結(jié)果
1
3
5
7
9
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python自動(dòng)化定位的9種函數(shù)方法小結(jié)
對于python進(jìn)行自動(dòng)化定位有9種方法,這篇文章主要來和大家聊聊這9種方法的具體使用,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2024-11-11
100 個(gè) Python 小例子(練習(xí)題一)
這篇文章主要介紹 Python 小例子,有數(shù)字組合、個(gè)稅計(jì)算、完全平方數(shù)、三數(shù)排序、斐波那契數(shù)列、copy、九九乘法表、暫停一秒輸出等多個(gè)實(shí)例,需要的朋友可以參考一下2021-10-10
利用Python進(jìn)行全面的GPU環(huán)境檢測與分析
這篇文章主要為大家詳細(xì)介紹了如何使用Python編寫一個(gè)強(qiáng)大的 GPU 診斷工具,它能夠全面收集和分析系統(tǒng)中的 GPU 相關(guān)信息,感興趣的可以了解下2025-01-01
Python實(shí)現(xiàn)TOPSIS分析法的示例代碼
TOPSIS法是一種常用的綜合評價(jià)方法,其能充分利用原始數(shù)據(jù)的信息,其結(jié)果能精確反應(yīng)各評價(jià)方案之間的差距。本文將利用Python實(shí)現(xiàn)這一方法,感興趣的可以了解一下2023-02-02
獲取python運(yùn)行輸出的數(shù)據(jù)并解析存為dataFrame實(shí)例
這篇文章主要介紹了獲取python運(yùn)行輸出的數(shù)據(jù)并解析存為dataFrame實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07

