版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Python練習(xí)筆記1. 簡(jiǎn)單循環(huán)#diceimport randomfor x in range(1,11): throw_1 = random.randint(1,6) throw_2 = random.randint(1,6) total = throw_1 + throw_2 print(total) if total = 7: print(Seven Thrown!) if total = 11: print(Eleven Thrown!) if throw_1 = throw_2: print(Double thrown!) if total = 5 and total = 5 a
2、nd total = 9:這句話還可以這樣寫(xiě)if not (total 9):另外,C里面的|(或)在Python里面是or【注意】print括號(hào)里面引用要打印的字符用單引號(hào),不是雙引號(hào);嚴(yán)格按照縮進(jìn)(縮進(jìn)在python里相當(dāng)于C里面的)。#dice_elifimport randomfor x in range(1,11): throw_1 = random.randint(1,6) throw_2 = random.randint(1,6) total = throw_1 + throw_2 print(total)if total 4: print(Bad luck!) elif tot
3、al book_name = Programming Raspberry Pi若直接輸入book_name若輸入print(book_name)#區(qū)別:第一個(gè)輸出一個(gè)字符串(帶引號(hào)),第二個(gè)打印一個(gè)值(不帶引號(hào))字符串長(zhǎng)度:len(book_name)獲取字符串中指定位置字符:book_name1截取字符串:book_name0:11把字符串加到一起:book_name + by Dolcerena【注意】數(shù)組下標(biāo)參數(shù)用方括號(hào);首字母的位置從0開(kāi)始;輸入下標(biāo)超出字符串長(zhǎng)度會(huì)報(bào)錯(cuò);截取時(shí)輸入第二個(gè)數(shù)字“11”,其實(shí)取到字符串的第10個(gè)字符;如果不確定取到哪里,可以12:(或者:12)這樣會(huì)默認(rèn)取
4、到最后(或者開(kāi)頭)。4.列表字符串是字符的列表。 numbers = 123,34,55,321,9給列表numbers賦初值(可以用len()得numbers的長(zhǎng)度:5) numbers1:3取numbers里的2,3項(xiàng) numbers0 = 1將numbers里的第一項(xiàng)用“1”覆蓋 numbers.sort()對(duì)numbers里的值進(jìn)行排序 numbers.pop()移除列表中的一項(xiàng),若括號(hào)中沒(méi)有聲明移除哪一項(xiàng),則默認(rèn)移除最后一項(xiàng) numbers.insert(1,66)在列表中增加一項(xiàng),1代表插入位置,66代表插入內(nèi)容 big_list = 123,hello,inner list,2,
5、True復(fù)合列表,結(jié)構(gòu)如下圖:big_list123“hello”inner list2True思考1:如何取出2? big_list21(將”big_list2”看做一個(gè)列表,列表后加1取出當(dāng)前列表第二項(xiàng),即2)思考2:設(shè)計(jì)一個(gè)for循環(huán),將列表中的項(xiàng)列出來(lái)list = 1,one,111for item in list: print(item)【注意】pop()括號(hào)中的參數(shù)也是從0開(kāi)始,即pop(1)移除列表中第二項(xiàng);其他也是。5.函數(shù)功能:創(chuàng)造一個(gè)函數(shù)make_polite,讓句子變得禮貌。#functiondef make_polite(sentence): polite_senten
6、ce = sentence + please return polite_sentenceprint(make_polite(pass the book)def:關(guān)鍵詞,后跟函數(shù)名,最后加冒號(hào)return:函數(shù)返回值,不是每個(gè)函數(shù)必須有的小游戲:吊死鬼第一步:從幾個(gè)單詞中挑選一個(gè)作為被猜單詞#hangman_pick_a_wordimport randomwords = period, record,field,object,physicaldef pick_a_word(): word_position = random.randint(0,len(words)-1) return word
7、sword_positionprint(pick_a_word()【這句話用來(lái)測(cè)試是否能夠隨機(jī)選出單詞,測(cè)完需刪掉】lives remaining = 14【全局變量剩余生命值】guessed_letters = 第二步:寫(xiě)出play函數(shù),再去寫(xiě)play中用到但還沒(méi)有定義的函數(shù)def play(): word = pick_a_word() while True: guess = get_guess(word) if process_guess(guess,word): print(You Win! Well Done!) break if lives_remaining = 0: print
8、(You are Hung!) print(The word was: + word) break第三步:完善get_gusss函數(shù),實(shí)現(xiàn)讀取玩家輸入字符def get_guess(word): print_word_with_blanks(word) print(Lives Remaining: + str(lives_remaining) guess = input(Guess a letter or whole word?) return guessdef print_word_with_blanks(word): print(print_word_with_blanks(word):n
9、ot done yet)【“還沒(méi)有完成”這只是個(gè)樁函數(shù),因?yàn)檫€沒(méi)來(lái)得及寫(xiě)】第四步:完善process_guess函數(shù)與print_word函數(shù)def print_word_with_blanks(word): display_word = #空的(沒(méi)有猜中任何一個(gè)字母)# for letter in word:#把輸入的字母與word里的單詞逐個(gè)比較# if guessed_letters.find(letter) -1: #letter found#內(nèi)建函數(shù)find,不同返回-1相同返回字母位置# display_word = display_word +letter else: #lett
10、er not found display_word = display_word + _ print(display_word)def process_guess(guess,word): global lives_remaining global guessed_letters lives_remaining = lives_remaining - 1 guessed_letters = guessed_letters + guess#把猜過(guò)的字母放入guessed_letters# return False第五步:兩個(gè)選擇,輸入一個(gè)字母或整個(gè)單詞把process_guess函數(shù)做大的改動(dòng)d
11、ef process_guess(guess,word): if len(guess) 1: return whole_word_guess(guess,word) else: return single_letter_guess(guess,word)定義單個(gè)字母函數(shù) def single_letter_guess(guess,word): global lives_remaining global guessed_letters if word.find(guess) = -1: #word guess was incorrect lives_remaining = lives_remai
12、ning - 1 guessed_letters = guessed_letters + guess if all_letters_guessed(word):#每次調(diào)用一遍,檢查是否已經(jīng)全部猜中# return Truereturn False def all_letters_guessed(word): for letter in word: if guessed_letters.find(letter) = -1: return False return True與整個(gè)單詞函數(shù)def whole_word_guess(guess,word): global lives_remaining
13、 if guess.lower() = word.lower():#把字母都轉(zhuǎn)化成小寫(xiě)后比較# return True else: lives_remaining = lives_remaining - 1 return False整個(gè)游戲:#hangmanimport randomlives_remaining = 14guessed_letters = words = period, record,field,object,physicaldef pick_a_word(): word_position = random.randint(0,len(words)-1) return wor
14、dsword_positiondef play(): word = pick_a_word() while True: guess = get_guess(word) if process_guess(guess,word): print(You Win! Well Done!) break if lives_remaining = 0: print(You are Hung!) print(The word was: + word) break def get_guess(word): print_word_with_blanks(word) print(Lives Remaining: +
15、 str(lives_remaining) guess = input(Guess a letter or whole word?) return guessdef print_word_with_blanks(word): display_word = for letter in word: if guessed_letters.find(letter) -1: #letter found display_word = display_word + letter else: #letter not found display_word = display_word + _ print(dis
16、play_word)def process_guess(guess,word): if len(guess) 1: return whole_word_guess(guess,word) else: return single_letter_guess(guess,word) def single_letter_guess(guess,word): global lives_remaining global guessed_letters if word.find(guess) = -1: #word guess was incorrect lives_remaining = lives_remaining - 1 guessed_letters = guessed_letters + guess if all_letters_guessed(word): return True return False def all_letters_gu
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年高職(鋼結(jié)構(gòu)工程技術(shù))鋼結(jié)構(gòu)工程施工試題及答案
- 2025年高職食品營(yíng)養(yǎng)與檢測(cè)(營(yíng)養(yǎng)配餐設(shè)計(jì))試題及答案
- 2025年本科云計(jì)算與大數(shù)據(jù)技術(shù)(云計(jì)算架構(gòu)設(shè)計(jì))試題及答案
- 2025年大學(xué)城市軌道交通工程技術(shù)(城軌工程設(shè)計(jì))試題及答案
- 2025年高職臨床醫(yī)學(xué)基礎(chǔ)(臨床基礎(chǔ)理論)試題及答案
- 內(nèi)墻施工方案八局-中國(guó)建設(shè)銀行濟(jì)南分行濼源大街辦公樓裝修改造項(xiàng)目
- 河北省秦皇島市2025年八年級(jí)上學(xué)期期末考試物理試題附答案
- 近七年北京中考語(yǔ)文試題及答案2025
- 2026年汕頭招商局港口集團(tuán)有限公司招聘?jìng)淇碱}庫(kù)參考答案詳解
- 養(yǎng)老院老人生活設(shè)施定期檢查制度
- 北京市租賃房屋治安責(zé)任書(shū)
- 2026年北京市公務(wù)員錄用考試申論試題及答案
- 醫(yī)院門(mén)診部2025年度工作總結(jié)及2026年工作計(jì)劃
- 2025年醫(yī)院停電應(yīng)急預(yù)案演練腳本
- AI在醫(yī)療質(zhì)量控制中的指標(biāo)優(yōu)化
- 2、公安檢查站治安管控系統(tǒng)解決方案
- 停車場(chǎng)電車起火應(yīng)急預(yù)案
- DB1310-T 369-2025 化學(xué)分析實(shí)驗(yàn)室玻璃儀器使用規(guī)
- 2025年四川蜀道高速公路集團(tuán)有限公司招聘工作人員考試筆試備考題庫(kù)及答案
- GB/T 25630-2010透平壓縮機(jī)性能試驗(yàn)規(guī)程
- GB/T 19610-2004卷煙通風(fēng)的測(cè)定定義和測(cè)量原理
評(píng)論
0/150
提交評(píng)論