您的当前位置:首页正文

python二级考试操作题4

来源:华佗健康网
1.根据输入正整数 n,作为财务数据,输出一个宽度为20 字符,n 右对齐显示,带千位分隔符的效果,使用减号字符“-”填充。如果输入正整数超过20 位,则按照真实长度输出。提示代码如下:

n =n =input()

____①____ #____①____ #可以多行

输入输出示例

输入

示例1 习题讲解参考代码

n =n =input()

# 请输入整数print(\"{:->20,}\".format(eval(n)))

2190000

输出

-----------2,190,000

2.PyInstaller 库可以对程序打包,给定一个Python 源程序文件a.py,图标文件为a.ico,将其打包为在Windows 平台上带有上述图标的单一可执行文件,使用什么样的命令?

print 这个命令即可自动评阅习题讲解参考代码

pyinstaller –pyinstaller –i a.i a.ico –ico –F a.F a.py

3.以123 为随机数种子,随机生成10 个在1 到999(含)之间的随机数,以逗号分隔,打印输出,请补充横线处代码。提示代码如下

import random 

____①____for____①____for i in i inrange(____②________②____):②____):

print(____③____,____③____, end= end=\)

习题讲解参考代码

import random 

random.random.seed(seed(123)for i in i inrange(10):

print(random.random.randint(randint(1,999), end= end=\)

4.使用 turtle 库的 turtle.right() 函数和 turtle.fd() 函数绘制一个菱形四边形,边长为 200 像素,效果如下图所示。请勿修改已经给出的第一行代码,并完善程序。

提示代码:

import turtle as turtle as t

本题暂不支持自动评阅,print('ok') 即可得分并查看答案。

习题讲解 参考代码

import turtle as turtle as t t.right(right(-30)for i in i in range(2):     t.    t.fd(fd(200)

    t.    t.right(right(60*(i+1))for i in i in range(2):     tfd(fd(200)     t.    t.right(right(60*(i+1))

5.补充完善如下代码,使得程序能够计算 a 中各元素与 b 逐项乘积的累加和。 提示代码如下:

a =a = [[1,2,3], [4,5,6], [7,8,9]] b =b = [3,6,9]

____①____for____①____for c in c in a: a:for j in j in ____②____:____②____:     s +=    s += c[ c[j]*b[j]print(s)

习题讲解 参考代码

a =a = [[1,2,3], [4,5,6], [7,8,9]] b =b = [3,6,9] s =s = 0for c in c in a: a:

    for    for j in j in range(3):

        s +=        s += c[ c[j]*b[j]print(s)

6.《命运》和《寻梦》都是著名科幻作家倪匡的科幻作品。这里给出一个《命运》和《寻梦》的网络版本,文件名为“命运-网络版.txt”和“寻梦-网络版.txt”。   

问题1:请编写程序,对这两个文本中出现的字符进行统计,字符与出现次数之间用冒号:分隔,将两个文件前 100 个最常用字符分别输出保存到“命运-字符统计.txt”和“寻梦-字符统计.txt”文件中,该文件要求采用 CSV 格式存储,参考格式如下(注意,不统计回车字符): 命:90, 运:80, 寻:70, 梦:60 (略)   

问题2:请编写程序,对“命运-字符统计.txt”和“寻梦-字符统计.txt”中出现的相同字符打印输出。“相同字符.txt”文件中,字符间使用逗号分隔。   

本题暂不支持自动评阅,print('ok') 即可得分并查看答案。 习题讲解 参考代码 1

names =names = [\"命运\", \"寻梦\"]for name in name in names: names:

    fi =    fi = open(name+name+\"-网络版.txt\", \"r\", encoding= encoding=\"utf-8\")     fo =    fo = open(name+name+\"-字符统计.txt\", \"w\", encoding= encoding=\"utf-8\")     txt =    txt = fi. fi.read()read()     d =    d = {}     for    for c in c in txt: txt:

        d[        d[c] = d. d.get(get(c, 0) + 1     del    del d[ d['\\n']

    ls =    ls = list(ditems())items())

    ls.    ls.sort(sort(key=key=lambda x: x:x[1], reverse= reverse=True)     for    for i in i in range(100):

        ls[        ls[i] = \"{}:{}\".format(ls[ls[i][0], ls[ ls[i][1])

    fo.    fo.write(write(\.join(join(ls[:ls[:100]))     fi.    fi.close()close()     fo.    fo.close()close()

参考代码 2

def getList(name):name):

    f =    f = open(name+name+\"-字符统计.txt\", \"r\", encoding= encoding=\"utf-8\")     words =    words = f. f.read().read().split(split(',')     for    for i in i in range(len(words)):words)):

        words[        words[i] = words[ words[i].split(split(':')[0]     f.    f.close()close()

    return    return wordsdef wordsdef main():     fo =    fo = open(\"相同字符.txt\", \"w\")     ls1 =    ls1 = getList( getList(\"命运\")     ls2 =    ls2 = getList( getList(\"寻梦\")     ls3 =    ls3 = []     for    for c in c in ls1: ls1:         if        if c in c in ls2: ls2:             ls3.            ls3.append(append(c)     fowrite(write(\join(join(ls3))ls3))     fo.    fo.close()close() main()main()

因篇幅问题不能全部显示,请点此查看更多更全内容