Python小测试.doc_第1页
Python小测试.doc_第2页
Python小测试.doc_第3页
Python小测试.doc_第4页
Python小测试.doc_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第六周 A.1. 每个类的定义必须包含一个初始化方法,该初始化方法的名称是什么? _init_(两边各2个下划线)2. Python语言中,函数和方法的主要区别是什么? 函数在类之外定义,而方法在类当中定义,方法是类的一部分。4.假设你有以下类和方法的定义(省略了部分代码):class My_Class: def my_method(self, value1, value2): 假设该方法有2个参数,该方法可以完成某种功能。 my_object = My_Class()最后一行定义了一个名称为my_object的变量,该变量是My_class类的一个对象。以下哪个是使用该对象 my_method 方法的正确语法? my_object.my_method(1, 2)5.我们希望小球具备移动的能力,以下哪个设计是正确的?class Ball: def _init_(self, pos, r): self.center = pos self.radius = r def move(self, move_vector): 通过加上给定矢量的分量来改变小球的位置 self.center0 += move_vector0 self.center1 += move_vector1# balls : 为Ball对象的列表balls = 6.多数面向对象的编程语言允许方法重载,即同一个方法名称可以因其参数的不同而出现多个版本。本题你将通过试验来体会什么是方法重载并验证Python是否支持重载。请运行以下Python代码:class Overload(object): def _init_(self, param1): pass def _init_(self, param1, param2): passobj1 = Overload(1)obj2 = Overload(1, 2)Overload类定义完成后,我们希望创建2个Overload对象,如果Python支持重载,你将能够使用1个参数创建一个Overload对象,也能够使用2个参数创建一个Overload对象。通过测试,Python是否支持重载? 不支持9.按照Python建议的编码格式要求(PEP 8),类的名称应当遵循首字母大写规则,以下哪些是符合要求的类名称?Student ImageInfo BankAccount10.Python语言中对象这一术语的含义是什么?请从下面的下拉式列表中选择正确的答案。根据类定义创建的一个具体实例 第六周 第一页 3.作为类定义的一个例子,请仔细阅读以下代码:# 游戏角色类的定义class Character: def _init_(self, name, initial_health): = name # 角色名称 self.health = initial_health # 健康值 self.inventory = # 装备 # 对象转字符串方法 def _str_(self): s = Name: + s += Health: + str(self.health) s += Inventory: + str(self.inventory) return s # 拿起某装备方法 def grab(self, item): self.inventory.append(item) # 获取健康值 def get_health(self): return self.health上述例子中self参数代表什么? Character类的一个对象实例7.首先在PyCharm中创建一个名称为“银行账户管理“的项目,在该项目中新建一个名称为Account的Python程序文件,然后完成以下类的定义:# -*- coding: utf-8 -*-class BankAccount: def _init_(self, initial_balance): 用指定的余额创建一个银行账户 self.balance = initial_balance def deposit(self, amount): 将指定金额存入该银行账户 self.balance += amount def withdraw(self, amount): 按指定金额从该银行账户取款。注意:余额不足不能取款 pass def get_balance(self): 返回该银行账户的当前余额 return self.balancedeposit和withdraw方法均会改变该银行账户的余额,调用withdraw方法时如果余额不足(即导致透支)将无法扣款。请实现该业务逻辑。下面几行代码如果运行后显示0,说明你定义的类是正确的:my_account = BankAccount(10)my_account.withdraw(5)my_account.deposit(10)my_account.withdraw(20)my_account.withdraw(15)print(my_account.get_balance()请将以下代码复制到你的程序文件的尾部,运行你的程序进行测试,将运行结果填在方框内。my_account = BankAccount(10)my_account.withdraw(5)my_account.deposit(10)my_account.withdraw(5) 第六周 第二页my_account.withdraw(15)my_account.deposit(20)my_account.withdraw(5)my_account.deposit(10)my_account.deposit(20)my_account.withdraw(15)my_account.deposit(30)my_account.withdraw(10)my_account.withdraw(15)my_account.deposit(10)my_account.withdraw(50)my_account.deposit(30)my_account.withdraw(15)my_account.deposit(10)my_account.withdraw(5)my_account.deposit(20)my_account.withdraw(15)my_account.deposit(10)my_account.deposit(30)my_account.withdraw(25)my_account.withdraw(5)my_account.deposit(10)my_account.withdraw(5)my_account.withdraw(15)my_account.deposit(10)my_account.withdraw(5)my_account.withdraw(15)my_account.deposit(10)my_account.withdraw(5)print(my_account.get_balance() 258.我们将继续使用上一题的BankAccount类,上一题BankAccount类的定义应当经得起该题的测试。一个银行如果只能管理一个账户,它将无法生存,该题我们要测试管理多个账户的能力。下面7行代码可以测试你定义的类是否满足管理多个账户的要求:account1 = BankAccount(10)account1.withdraw(15)account2 = BankAccount(15)account2.deposit(10)account1.deposit(20)account2.withdraw(20)print(account1.get_balance(), account2.get_balance()以上测试代码应当在终端输出 30 和 5 两个数字。请将以下测试复制到你的程序文件的尾部,运行你的程序进行测试,观察多个账户经过多次存款、取款操作后结果是什么。account1 = BankAccount(20)account1.deposit(10)account2 = BankAccount(10)account2.deposit(10)account2.withdraw(50)account1.withdraw(15)account1.withdraw(10)account2.deposit(30) 第六周 第三页account2.withdraw(15)account1.deposit(5)account1.withdraw(10)account2.withdraw(10)account2.deposit(25)account2.withdraw(15)account1.deposit(10)account1.withdraw(50)account2.deposit(25)account2.deposit(25)account1.deposit(30)account2.deposit(10)account1.withdraw(15)account2.withdraw(10)account1.withdraw(10)account2.deposit(15)account2.deposit(10)account2.withdraw(15)account1.deposit(15)account1.withdraw(20)account2.withdraw(10)account2.deposit(5)account2.withdraw(10)account1.deposit(10)account1.deposit(20)account2.withdraw(10)account2.deposit(5)account1.withdraw(15)account1.withdraw(20)account1.deposit(5)account2.deposit(10)account2.deposit(15)account2.deposit(20)account1.withdraw(15)account2.deposit(10)account1.deposit(25)account1.deposit(15)account1.deposit(10)account1.withdraw(10)account1.deposit(10)account2.deposit(20)account2.withdraw(15)account1.withdraw(20)account1.deposit(5)account1.deposit(10)account2.withdraw(20)print(account1.get_balance(), account2.get_balance()以上测试代码应当依次在终端输出两个数字,请将你看到的两个数字填入下面的输入框(用空格隔开两个数字)。55 115 第六周 第四页 B.1.本周的“拼图”游戏中画布的宽度,高度和图像块的边长分别是。这些全局变量的值(以像素为单位)为: 600,700, 200 2.本周的“拼图”游戏中画布上有几个图像块: 83.本周的“拼图”游戏中空白位置用什么表示: None None4.将鼠标点击位置换算成拼图板上横坐标的方法为: int(pos0 / IMAGE_SIZE) 5.在类定义的_init_方法中,新对象应该由什么代码返回? _init_方法中不需要return语句6.要读懂一段代码,方法很多。你可以尝试用其它不同的代码来实现这段代码的功能,也就是对于相同的起始值,两段代码返回或处理后的结果完全一样。以下代码定义了一个合并多个列表的函数,这是一种实现方法。例如,list_extend_many(1,2,3,4,5,6,7)返回1, 2, 3, 4, 5, 6, 7 ,该函数不会修改任何参数指向的对象。def list_extend_many(lists): 参数为元素为列表的列表,返回一个合并后的列表 result = for l in lists: result.extend(l) return result def list_extend_many(lists): def list_extend_many(lists): result = result = i = 0 i = 0 while i len(lists): while i = 0: while n 0: # 假设这里没有修改n # 假设这里没有修改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 range(3): for i in range(3): m0, m2 = m2, m0 temp = m0i m0i, m2i = m2i, m0i m0i = m2i m2i =

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论