第4单元 引用,让编程事半功倍《python程序编写入门》(高教版)章节过关卷(四)

2025-04-10
| 2份
| 17页
| 100人阅读
| 0人下载

内容正文:

《Python 程序编写入门》高等教育出版社 第4单元 引用,让编程事半功倍-自定义模块 时间:90分钟 总分:100分 班级___________ 姓名___________ 学号___________ 成绩_____________ 一、选择题(本大题共10小题,每小题2分,共20分) 1.test.py 与 index.py 在同一个文件夹中,test.py 文件中自定义hello函数,下列在 index.py 文件中引入 test 模块正确的是( )。 A. from test B. from test import hello C. from test import * D. import test 2.test.py 与 index.py 在同一个文件夹中,test.py 文件中自定义 hello 函数,下列在 index.py 文件中引入 hello 函数正确的是( )。 A. from test B. from test import hello C. from test import * D. import test 3.test.py 与 index.py 在同一个文件夹中,test.py 文件中自定义hello函数,下列在 index.py 文件中引入 hello 函数并命名别名为he。下面选项正确的是( )。 A. import test as he B. from test import he C. import test from hello as he D. from test import hello as he 4.test.py 与 index.py 在同一个文件夹中,test.py 文件中自定义hello和world 函数,下列在 index.py 文件中引入 hello和world 函数正确的是( )。 A. from test import hello,world B. from test import hello world C. import test hello,world D. import test hello world 5.test.py 与 index.py 在同一个文件夹中,test.py 文件中自定义 hello和world 函数,下列在 index.py 文件中引入hello和world函数并分别取别名为he和wd 。下面选项正确的是( )。 A. import test hello as he,world as wd B. from test import hello as he world as wd C. from test import hello as he,world as wd D. import test hello as he world as wd 6.下列标准自定义函数名命名正确的是( ) A.中国 C 3he B._3w* D. w u o 7.下列引用模块不合法的是()。 A.import guo C.from guo import * B.import guo as w D.from guo import 8.下列用于处理日历功能标准库的是( ) A. re B. os C. random D. calendar 9.下列用于处理文件和目录相关操作的标准库是( ) A. os B. re C. sys D. math 10.下列用于处理正则表达式的标准库是( ) A. os B. re C. sys D. math 二、填空题(本大题共5小题,每小题2分,共10分) 1.使用 _______ 关键字可以给导入的模块指定别名。 2.使用 _______ 关键字可以导入指定模块。 3._______ 标准库用于处理解释器相关的操作。 4.从模块中导入指定函数必须要用到的关键字是 _______。 5._______ 标准库用于处理数学相关的运算。 三、程序分析题(本大题共10小题 共42分) 1.test.py 与 index.py 在同一个文件夹中,test.py 文件中自定义hello函数,在 index.py 文件中引入 test模块实现“猜数字游戏”。根据以上程序设计要求,完成下列程序填空,并把答案写在相应的序号处。 test.py 文件中的程序代码如下: def ______①_______: import random target = random. ______②_______ (1, 1000) s = eval(input("请输入一个1至1000范围内的整数:")) if s > 1000 or s < 1: print("你输入的数不合法,不在1至1000的范围") elif target > s: print("猜小了") elif target < s: print("猜大了") elif target _____③_______ s: print("猜对了") index.py文件中的程序代码如下: import test ______④_______.hello() ①______________ ②______________ ③______________ ④______________(每空2分) 2.test.py 与 index.py 在同一个文件夹中,test.py 文件中自定义 hello 函数,在 index.py 文件中引入 test 模块实现“登录程序”。根据以上程序设计要求,完成下列程序填空,并把答案写在相应的序号处。 test.py 文件中的程序代码如下: def hello(a,b): if ______①_______: print("登录成功!") if a != "yao" and b != "123456": print("用户名及密码都错误!") elif a != "yao" and b == "123456": print("用户名错误!") elif ______②_______: print("密码错误!") index.py文件中的程序代码如下: import test as t usr=input("请输入用户名: ") pwd=input("请输入密码: ") ______③_______.hello(______④_______) ①______________ ②______________ ③______________ ④______________(每空2分) 3.test.py 与 index.py 在同一个文件夹中,test.py 文件中自定义 hello 函数,在 index.py 文件中引入test模块实现“考试等级”。根据以上程序设计要求,完成下列程序填空,并把答案写在相应的序号处。 test.py 文件中的程序代码如下: def hello( ): s = eval(input("请输入一个考试成绩:")) if s >= 80 and __________ print("考试等级为:A") ______②_______ s >= 70 and s < 80: print("考试等级为:B") elif s >= 60 and s < 70: print("考试等级为:C") elif s < 60: print("考试等级为:D") index.py 文件中的程序代码如下: ______③_______ test import hello ______④_______ t( ) ①______________ ②______________ ③______________ ④______________(每空2分) 4.实现倒计时关机效果。根据下面程序设计的要求,完成下列程序填空,并把答案写在相应的序号处。 from time import sleep from os import popen n=eval(input("请输入倒计时秒数:")) print("倒计时开始:") while True: print("还剩",n,"秒") ______①_______ n=n-1 if n in [1,2]: print("警告,马上关机!") if n==0: print("***时间到了!开始关机!***") ______②_______ ①___________ ②___________ (每空3分) 5.实现“回文”判断的功能。完成下列程序填空,并把答案写在相应的序号处。 def f(s1): n=len(s1)-1 s2="" while n>=0: ______①_______ n=n-1 if s1==s2: return "是回文" else: return "不是回文" while True: s=input("请输入一个字符串(0 退出):") if s=="0": break else: print(s, ______②_______) ①___________ ②___________ (每空3分) 6.实现判断素数的功能。完成下列程序填空,并把答案写在相应的序号处。 def f(n): for i in ______①_______: if n%i==0: print(n,"不是素数") break ______②_______: print(n,"是素数") s=int(input(“请输入一个数:”)) f(s) ①___________ ②___________ (每空3分) 四、程序设计题(本大题共3小题 共28分) 1.程序设计题:设计自定义函数,函数功能为计算 n! 的运算结果。(8分) 2.程序设计题:设计自定义函数,函数功能为获取斐波那契数列中的第 n 项数并返回。(斐波那契数列:0,1,1,2,3,5,8,13,21,34,……)(8分) 3.程序设计题:设计自定义函数,函数功能为显示输出 n 以内的所有素数,以空格分隔。(12分) 学科网(北京)股份有限公司 $$ 《Python 程序编写入门》高等教育出版社 第4单元 引用,让编程事半功倍-自定义模块 时间:90分钟 总分:100分 班级___________ 姓名___________ 学号___________ 成绩_____________ 一、选择题(本大题共10小题,每小题2分,共20分) 1.test.py 与 index.py 在同一个文件夹中,test.py 文件中自定义hello函数,下列在 index.py 文件中引入 test 模块正确的是( )。 A. from test B. from test import hello C. from test import * D. import test 【答案】D 【解析】要在index.py中引入test模块,正确的语法是import test。这样可以使用test.hello()来调用test模块中的hello函数。 2.test.py 与 index.py 在同一个文件夹中,test.py 文件中自定义 hello 函数,下列在 index.py 文件中引入 hello 函数正确的是( )。 A. from test B. from test import hello C. from test import * D. import test 【答案】B 【解析】要在index.py中直接调用test模块中的hello函数,正确的语法是from test import hello。这样可以直接使用hello()调用函数。 3.test.py 与 index.py 在同一个文件夹中,test.py 文件中自定义hello函数,下列在 index.py 文件中引入 hello 函数并命名别名为he。下面选项正确的是( )。 A. import test as he B. from test import he C. import test from hello as he D. from test import hello as he 【答案】D 【解析】要在index.py中引入test模块的hello函数并命名为he,正确的语法是from test import hello as he。这样可以使用he()调用函数。 4.test.py 与 index.py 在同一个文件夹中,test.py 文件中自定义hello和world 函数,下列在 index.py 文件中引入 hello和world 函数正确的是( )。 A. from test import hello,world B. from test import hello world C. import test hello,world D. import test hello world 【答案】A 【解析】要在index.py中引入test模块的hello和world函数,正确的语法是from test import hello,world。这样可以直接使用hello()和world()调用函数。 5.test.py 与 index.py 在同一个文件夹中,test.py 文件中自定义 hello和world 函数,下列在 index.py 文件中引入hello和world函数并分别取别名为he和wd 。下面选项正确的是( )。 A. import test hello as he,world as wd B. from test import hello as he world as wd C. from test import hello as he,world as wd D. import test hello as he world as wd 【答案】C 【解析】要在index.py中引入test模块的hello和world函数并分别命名为he和wd,正确的语法是from test import hello as he,world as wd。这样可以使用he()和wd()调用函数。 6.下列标准自定义函数名命名正确的是( ) A.中国 C 3he B._3w* D. w u o 【答案】A 【解析】标准自定义函数名不能以数字开头,不能包含特殊字符,只能包含字母、数字和下划线。选项A符合这些规则 7.下列引用模块不合法的是()。 A.import guo C.from guo import * B.import guo as w D.from guo import 【答案】D 【解析】from guo import是不合法的,因为import后面需要指定要导入的函数或变量名。 8.下列用于处理日历功能标准库的是( ) A. re B. os C. random D. calendar 【答案】D 【解析】calendar标准库用于处理日历功能。 9.下列用于处理文件和目录相关操作的标准库是( ) A. os B. re C. sys D. math 【答案】A 【解析】os标准库用于处理文件和目录相关操作。 10.下列用于处理正则表达式的标准库是( ) A. os B. re C. sys D. math 【答案】B 【解析】re标准库用于处理正则表达式 二、填空题(本大题共5小题,每小题2分,共10分) 1.使用 _______ 关键字可以给导入的模块指定别名。 【答案】as 【解析】使用as关键字可以给导入的模块指定别名 2.使用 _______ 关键字可以导入指定模块。 【答案】import 【解析】使用import关键字可以导入指定模块。 3._______ 标准库用于处理解释器相关的操作。 【答案】sys 【解析】sys标准库用于处理解释器相关的操作 4.从模块中导入指定函数必须要用到的关键字是 _______。 【答案】from 【解析】从模块中导入指定函数必须要用到的关键字是from 5._______ 标准库用于处理数学相关的运算。 【答案】math 【解析】math标准库用于处理数学相关的运算。 三、程序分析题(本大题共10小题 共42分) 1.test.py 与 index.py 在同一个文件夹中,test.py 文件中自定义hello函数,在 index.py 文件中引入 test模块实现“猜数字游戏”。根据以上程序设计要求,完成下列程序填空,并把答案写在相应的序号处。 test.py 文件中的程序代码如下: def ______①_______: import random target = random. ______②_______ (1, 1000) s = eval(input("请输入一个1至1000范围内的整数:")) if s > 1000 or s < 1: print("你输入的数不合法,不在1至1000的范围") elif target > s: print("猜小了") elif target < s: print("猜大了") elif target _____③_______ s: print("猜对了") index.py文件中的程序代码如下: import test ______④_______.hello() ①______________ ②______________ ③______________ ④______________(每空2分) 【答案】① hello( ) ② randint ③ == ④ test 【解析】在test.py中定义hello函数,使用random.randint(1, 1000)生成随机数,比较输入的数字与随机数,判断猜对、猜大或猜小。在index.py中使用test.hello()调用函数。 2.test.py 与 index.py 在同一个文件夹中,test.py 文件中自定义 hello 函数,在 index.py 文件中引入 test 模块实现“登录程序”。根据以上程序设计要求,完成下列程序填空,并把答案写在相应的序号处。 test.py 文件中的程序代码如下: def hello(a,b): if ______①_______: print("登录成功!") if a != "yao" and b != "123456": print("用户名及密码都错误!") elif a != "yao" and b == "123456": print("用户名错误!") elif ______②_______: print("密码错误!") index.py文件中的程序代码如下: import test as t usr=input("请输入用户名: ") pwd=input("请输入密码: ") ______③_______.hello(______④_______) ①______________ ②______________ ③______________ ④______________(每空2分) 【答案】① a == "yao" and b == "123456" ② a == "yao" and b != "123456" ③ t ④ usr,pwd 【解析】在test.py中定义hello函数,判断用户名和密码是否正确,输出相应的提示信息。在index.py中使用t.hello(usr,pwd)调用函数。 3.test.py 与 index.py 在同一个文件夹中,test.py 文件中自定义 hello 函数,在 index.py 文件中引入test模块实现“考试等级”。根据以上程序设计要求,完成下列程序填空,并把答案写在相应的序号处。 test.py 文件中的程序代码如下: def hello( ): s = eval(input("请输入一个考试成绩:")) if s >= 80 and __________ print("考试等级为:A") ______②_______ s >= 70 and s < 80: print("考试等级为:B") elif s >= 60 and s < 70: print("考试等级为:C") elif s < 60: print("考试等级为:D") index.py 文件中的程序代码如下: ______③_______ test import hello ______④_______ t( ) ①______________ ②______________ ③______________ ④______________(每空2分) 【答案】① s <= 100: ② elif ③ from ④ as t 【解析】在test.py中定义hello函数,根据输入的成绩判断考试等级。在index.py中使用from test import hello as t导入函数,并调用t()。 4.实现倒计时关机效果。根据下面程序设计的要求,完成下列程序填空,并把答案写在相应的序号处。 from time import sleep from os import popen n=eval(input("请输入倒计时秒数:")) print("倒计时开始:") while True: print("还剩",n,"秒") ______①_______ n=n-1 if n in [1,2]: print("警告,马上关机!") if n==0: print("***时间到了!开始关机!***") ______②_______ ①___________ ②___________ (每空3分) 【答案】① sleep(1) ② break 【解析】使用time.sleep(1)实现秒数的延迟,当倒计时结束时使用break退出循环。 5.实现“回文”判断的功能。完成下列程序填空,并把答案写在相应的序号处。 def f(s1): n=len(s1)-1 s2="" while n>=0: ______①_______ n=n-1 if s1==s2: return "是回文" else: return "不是回文" while True: s=input("请输入一个字符串(0 退出):") if s=="0": break else: print(s, ______②_______) ①___________ ②___________ (每空3分) 【答案】① s2=s2+s1[n] ② f(s) 【解析】通过反转字符串判断是否为回文,使用f(s)调用函数判断输入的字符串。 6.实现判断素数的功能。完成下列程序填空,并把答案写在相应的序号处。 def f(n): for i in ______①_______: if n%i==0: print(n,"不是素数") break ______②_______: print(n,"是素数") s=int(input(“请输入一个数:”)) f(s) ①___________ ②___________ (每空3分) 【答案】① range(2,n) ② else 【解析】使用循环判断是否为素数,如果循环结束没有找到因数,则输出是素数。 四、程序设计题(本大题共3小题 共28分) 1.程序设计题:设计自定义函数,函数功能为计算 n! 的运算结果。(8分) 【答案】 def jie(n): if n==1: return 1 else: return n*jie(n-1) n=int(input("请输入正整数:")) print(jie(n)) 【解析】递归计算n的阶乘,输入正整数n,输出其阶乘结果。 2.程序设计题:设计自定义函数,函数功能为获取斐波那契数列中的第 n 项数并返回。(斐波那契数列:0,1,1,2,3,5,8,13,21,34,……)(8分) 【答案】 def f(n): if n==1: return 0 elif n==2: return 1 else: return f(n-1)+f(n-2) n=int(input("请输入数列的第n项:")) print(f(n)) 【解析】递归计算斐波那契数列的第n项,输入n,输出对应的数值 3.程序设计题:设计自定义函数,函数功能为显示输出 n 以内的所有素数,以空格分隔。(12分) 【答案】 def su(n): flag=True for i in range(2,n): if n%i==0: flag=False break return flag n=int(input("请输入正整数:")) for i in range(2,n+1): if su(i): print(i,end=' ') 【解析】定义函数判断是否为素数,输入正整数n,输出n以内的所有素数 学科网(北京)股份有限公司 $$

资源预览图

第4单元 引用,让编程事半功倍《python程序编写入门》(高教版)章节过关卷(四)
1
第4单元 引用,让编程事半功倍《python程序编写入门》(高教版)章节过关卷(四)
2
第4单元 引用,让编程事半功倍《python程序编写入门》(高教版)章节过关卷(四)
3
所属专辑
由于学科网是一个信息分享及获取的平台,不确保部分用户上传资料的 来源及知识产权归属。如您发现相关资料侵犯您的合法权益,请联系学科网,我们核实后将及时进行处理。