python第八周小测试_Python小测试.doc
Python小測試.doc
第六周 A.......1. 每個類的定義必須包含一個初始化方法,該初始化方法的名稱是什么 __init__(兩邊各2個下劃線)2. Python語言中,函數(shù)和方法的主要區(qū)別是什么 函數(shù)在類之外定義,而方法在類當(dāng)中定義,方法是類的一部分。4.假設(shè)你有以下類和方法的定義(省略了部分代碼)class My_Class def my_self, value1, value2 假設(shè)該方法有2個參數(shù),該方法可以完成某種功能。 my_object My_Class最后一行定義了一個名稱為my_object的變量,該變量是My_class類的一個對象。以下哪個是使用該對象 my_ 方法的正確語法 my_object.my_1, 25.我們希望小球具備移動的能力,以下哪個設(shè)計是正確的class Ball def __init__self, pos, r self.center pos self.radius r def moveself, move_vector 通過加上給定矢量的分量來改變小球的位置 self.center0 move_vector0 self.center1 move_vector1 balls 為Ball對象的列表balls 6.多數(shù)面向?qū)ο蟮木幊陶Z言允許方法重載,即同一個方法名稱可以因其參數(shù)的不同而出現(xiàn)多個版本。本題你將通過試驗來體會什么是方法重載并驗證Python是否支持重載。請運行以下Python代碼class Overloadobject def __init__self, param1 pass def __init__self, param1, param2 passobj1 Overload1obj2 Overload1, 2Overload類定義完成后,我們希望創(chuàng)建2個Overload對象,如果Python支持重載,你將能夠使用1個參數(shù)創(chuàng)建一個Overload對象,也能夠使用2個參數(shù)創(chuàng)建一個Overload對象。通過測試,Python是否支持重載 不支持9.按照Python建議的編碼格式要求(PEP 8),類的名稱應(yīng)當(dāng)遵循首字母大寫規(guī)則,以下哪些是符合要求的類名稱Student ImageInfo BankAccount10.Python語言中對象這一術(shù)語的含義是什么請從下面的下拉式列表中選擇正確的答案。根據(jù)類定義創(chuàng)建的一個具體實例 第六周 第一頁 3.作為類定義的一個例子,請仔細(xì)閱讀以下代碼 游戲角色類的定義class Character def __init__self, name, initial_health self.name name 角色名稱 self.health initial_health 健康值 self.inventory 裝備 對象轉(zhuǎn)字符串方法 def __str__self s Name self.name s Health strself.health s Inventory strself.inventory return s 拿起某裝備方法 def grabself, item self.inventory.appenditem 獲取健康值 def get_healthself return self.health上述例子中self參數(shù)代表什么 Character類的一個對象實例7.首先在PyCharm中創(chuàng)建一個名稱為“銀行賬戶管理“的項目,在該項目中新建一個名稱為Account的Python程序文件,然后完成以下類的定義 -*- coding utf-8 -*-class BankAccount def __init__self, initial_balance 用指定的余額創(chuàng)建一個銀行賬戶 self.balance initial_balance def depositself, amount 將指定金額存入該銀行賬戶 self.balance amount def withdrawself, amount 按指定金額從該銀行賬戶取款。注意余額不足不能取款 pass def get_balanceself 返回該銀行賬戶的當(dāng)前余額 return self.balancedeposit和withdraw方法均會改變該銀行賬戶的余額,調(diào)用withdraw方法時如果余額不足(即導(dǎo)致透支)將無法扣款。請實現(xiàn)該業(yè)務(wù)邏輯。下面幾行代碼如果運行后顯示0,說明你定義的類是正確的my_account BankAccount10my_account.withdraw5my_account.deposit10my_account.withdraw20my_account.withdraw15printmy_account.get_balance請將以下代碼復(fù)制到你的程序文件的尾部,運行你的程序進(jìn)行測試,將運行結(jié)果填在方框內(nèi)。my_account BankAccount10my_account.withdraw5my_account.deposit10my_account.withdraw5 第六周 第二頁my_account.withdraw15my_account.deposit20my_account.withdraw5my_account.deposit10my_account.deposit20my_account.withdraw15my_account.deposit30my_account.withdraw10my_account.withdraw15my_account.deposit10my_account.withdraw50my_account.deposit30my_account.withdraw15my_account.deposit10my_account.withdraw5my_account.deposit20my_account.withdraw15my_account.deposit10my_account.deposit30my_account.withdraw25my_account.withdraw5my_account.deposit10my_account.withdraw5my_account.withdraw15my_account.deposit10my_account.withdraw5my_account.withdraw15my_account.deposit10my_account.withdraw5printmy_account.get_balance 258.我們將繼續(xù)使用上一題的BankAccount類,上一題BankAccount類的定義應(yīng)當(dāng)經(jīng)得起該題的測試。一個銀行如果只能管理一個賬戶,它將無法生存,該題我們要測試管理多個賬戶的能力。下面7行代碼可以測試你定義的類是否滿足管理多個賬戶的要求account1 BankAccount10account1.withdraw15account2 BankAccount15account2.deposit10account1.deposit20account2.withdraw20printaccount1.get_balance, account2.get_balance以上測試代碼應(yīng)當(dāng)在終端輸出 30 和 5 兩個數(shù)字。請將以下測試復(fù)制到你的程序文件的尾部,運行你的程序進(jìn)行測試,觀察多個賬戶經(jīng)過多次存款、取款操作后結(jié)果是什么。account1 BankAccount20account1.deposit10account2 BankAccount10account2.deposit10account2.withdraw50account1.withdraw15account1.withdraw10account2.deposit30 第六周 第三頁account2.withdraw15account1.deposit5account1.withdraw10account2.withdraw10account2.deposit25account2.withdraw15account1.deposit10account1.withdraw50account2.deposit25account2.deposit25account1.deposit30account2.deposit10account1.withdraw15account2.withdraw10account1.withdraw10account2.deposit15account2.deposit10account2.withdraw15account1.deposit15account1.withdraw20account2.withdraw10account2.deposit5account2.withdraw10account1.deposit10account1.deposit20account2.withdraw10account2.deposit5account1.withdraw15account1.withdraw20account1.deposit5account2.deposit10account2.deposit15account2.deposit20account1.withdraw15account2.deposit10account1.deposit25account1.deposit15account1.deposit10account1.withdraw10account1.deposit10account2.deposit20account2.withdraw15account1.withdraw20account1.deposit5account1.deposit10account2.withdraw20printaccount1.get_balance, account2.get_balance以上測試代碼應(yīng)當(dāng)依次在終端輸出兩個數(shù)字,請將你看到的兩個數(shù)字填入下面的輸入框(用空格隔開兩個數(shù)字)。55 115 第六周 第四頁 B.........1.本周的“拼圖”游戲中畫布的寬度,高度和圖像塊的邊長分別是。這些全局變量的值(以像素為單位)為 600,700, 200 2.本周的“拼圖”游戲中畫布上有幾個圖像塊 83.本周的“拼圖”游戲中空白位置用什么表示 None None4.將鼠標(biāo)點擊位置換算成拼圖板上橫坐標(biāo)的方法為 intpos0 // IMAGE_SIZE 5.在類定義的__init__方法中,新對象應(yīng)該由什么代碼返回 __init__方法中不需要return語句6.要讀懂一段代碼,方法很多。你可以嘗試用其它不同的代碼來實現(xiàn)這段代碼的功能,也就是對于相同的起始值,兩段代碼返回或處理后的結(jié)果完全一樣。以下代碼定義了一個合并多個列表的函數(shù),這是一種實現(xiàn)方法。例如,list_extend_many1,2,3,4,5,6,7返回1, 2, 3, 4, 5, 6, 7 ,該函數(shù)不會修改任何參數(shù)指向的對象。def list_extend_manylists 參數(shù)為元素為列表的列表,返回一個合并后的列表 result for l in lists result.extendl return result def list_extend_manylists def list_extend_manylists result result i 0 i 0 while i lenlists while i lenlists result listsi result.extendlistsi i 1 i 1 return result return result 7.如果Python沒有超時終止機(jī)制,并且假設(shè)循環(huán)體內(nèi)省略的代碼中不包含break和return語句,以下哪些程序?qū)⒂肋h(yuǎn)運行無法終止你可以在每個循環(huán)中添加print語句來觀察和理解這些程序的行為。n 127834876 n 1 while n 0 while n 0 假設(shè)這里沒有修改n 假設(shè)這里沒有修改n. n // 2 n 1 8.以下哪些代碼可以交換列表m的第1和第3個元素 m 1, 2, 3,4, 5, 6,7, 8, 9 m 1, 2, 3,4, 5, 6,7, 8, 9 m 1, 2, 3,4, 5, 6,7, 8, 9 for i in range3 for i in range3 m0, m2 m2, m0 temp m0i m0i, m2i m2i, m0i m0i m2i m2i temp 9.我們可以用循環(huán)迭代來模擬生物進(jìn)化中的自然選擇現(xiàn)象。請編寫程序來計算兩種“獅頭象”(游戲中的生物,自然界并不存在這種生物)不同年代的種群數(shù)量。第年,慢“獅頭象”有1000只,快“獅頭象”有且僅有1只,快“獅頭象”是由一個基因突變產(chǎn)生的,其優(yōu)勢就是可以更好地避開捕食者。每年每個“獅頭象”會繁殖1個后代(暫不考慮有性繁殖的其它細(xì)節(jié))。此后再沒有發(fā)生任何基因突變,即慢“獅頭象”的后代均為慢“獅頭象”,而快“獅頭象”的后代均為快“獅頭象”。每年慢“獅頭象”的死亡率為40、快“獅頭象”的死亡率為30。因此,第年年初慢“獅頭象”有1000只,繁殖了1000只慢“獅頭象”,共計有2000只慢“獅頭象”,其中40(800只)死亡,第年年末剩余1200只慢“獅頭象”。而第年年初快“獅頭象”有1只,繁殖了1只快“獅頭象”,共計有2只快“獅頭象”,其中30(0.6只)死亡,第年年末剩余1.4只快“獅頭象”(為了簡化問題,我們允許種群的個體數(shù)量包含小數(shù))。年度慢“獅頭象”快“獅頭象”110001212001.4314401.96.........請注意,上面表中的種群個體數(shù)量是每年的年初數(shù)字,年末這些數(shù)值會發(fā)生變化。從第幾年開始,快“獅頭象”的數(shù)量超過了慢“獅頭象”請在下列文本框中輸入你的答案。 4510.在Python語言中,定義一個類所使用的保留字(關(guān)鍵詞)是什么請在下列輸中入框填入你的答案。 class
總結(jié)
以上是生活随笔為你收集整理的python第八周小测试_Python小测试.doc的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 本地yum安装
- 下一篇: Linux 配置iso系统盘为本地yum