二级python操作模拟题9答案.docx
1参考代码n = input( ) nums = n.split( )s = 0for i in nums:s += eval(i) print(s)2s = inputo #请输入一个由1和0组成的二进制数字串:d = 0while s:d = d*2 + (ord(s0) -ord('01)S = Sl:print("转换成十进制数是:()".format(d)#需要掌握print和format的格式用法,字符串的内置处理函数3 答案:fi = open("data.txt", 1r1)for 1 in fi:1 = l.splitc/)5 = 0.0n = len(l)for cours in 1:items = cours.split(1:')s += eval(itemsl)print("总和是:,平均值是: : 2f H .format(s s/n)fi.close()要点:1 .文件打开与关闭.按行读入的是字符串,需要用split ()分割2 .文件里有多行,用一个多重循环.对读进来的数据做运算,还要进一步的按照":”来分割,结果是列表,并且分 数在第二个字段这些都是细节,需要熟练掌握处理的步骤和函数4import turtlefor i in range(4):turtle.circle( 90,90)turtle.right(180)参考代码def is_prime(n):for i in range(2Jn):if n % i = 0:return Falsereturn TrueIs =23,45,78,87,11,67,89,13,243,56,67,31工,431,111,141for i in ls.copy():if is_prime(i) = True:Is.remove(i)print(len(ls)3 import jiebawith open(Msgld.txtM/'r"encoding =',utf-8,)as f:Issgld = f.readlines()for Is in Issgld: wordlist = jieba.cut(ls) for word in wordlist:if ”人,,in word:n = n + 1print(“人:" + str(n) + “次")