print(' '*2*(6-i),end='') for j in range(i, 0, -1): print(j, end = \print()
18. 下面程序输入>>>rev(‘I love you’)返回值为you love I def rev1(s): s=s.split()
s1=‘ ’.join(reversed(s))
return s1
19. 阅读程序,打印结果是1000 defaddInterest(balance,rate): newBalance=balance*(1+rate) balance=newBalance def main(): amount=1000 rate=0.05
addInterest(amount,rate) print (amount) main()
20. 阅读程序,打印结果是 def demo(newitem,old_list=[]): old_list.append(newitem) returnold_list def main():
print(demo(‘a’)) print(demo(‘b’)) main() 答案:['a']
['a', 'b']
21. 阅读程序,打印结果是
def func5(a, b, *c): print(a,b)
print(\ func5(1,2,3,4,5,6) 答案:1 2
length of c is 4,c is (3, 4, 5, 6) 22. 阅读程序,打印结果是(2.5, 3, 4) def demo(*para):
avg = sum(para)/len(para)
g = [i for i in para if i>avg] return (avg,)+tuple(g) print(demo(1,2,3,4))
23. 阅读程序,打印结果是
def f(w=1,h=2): print(w,h) f() f(w=3)
f(h=7) f(a=3) 答案:1 2 3 2 1 7 错误
24. 阅读程序,打印结果是
def sort(number1,number2): if number1 n1,n2=sort(3,2) print('n1 is ',n1) print('n2 is ',n2) 答案:n1 is 2 n2 is 3 25. 阅读程序,打印结果是10 60 def demo(m, n): if m>n: m, n = n, m p = m*n while m!=0: r = n%m n = m m = r return (n,p//n) print( demo(20, 30)) 三、程序填空题 1. 下面代码的功能是,随机生成50个介于[1,20]之间的整数,然后统计每个整数出现频率。请把 缺少的代码补全。 import random x = [random.____________(1,20) for i in range(_______)] r = dict() fori in x: r[i] = r.get(i, _____)+1 for k, v in r.items(): print(k, v) 答: 分别填写randint、50、0 2. 生成包含1000个随机字符的字符串,然后统计前20个高频字符,最后以15个位左对齐输出高 频字符,以5个位右对齐输出个数。请把缺少的代码补全。 importrandom x = string.ascii_letters + string.digits + string.punctuation y = [for iin range(1000)] d = dict() forchin y: d[ch] = d.get(ch, 0) + 1 items=list(d.items()) items.sort(,reverse=True) foriin range(20): word,count = items[i] print(%(word,count)) 答案:import string、random.choice(x)、key= lambda x:x[1]、“%-15s]” 3. 用户输入5,打印如右图字符金字塔图形,请补全代码: n=input('输入行数:') fori in range(1,n+1): print(,end=‘ ') for j in range(1,2*i): print('&',) print() 答案:n=int(n),' '*(n-i),end='' 4. 质因数分解,如输入60,则得到60 = 2 * 2 * 3 * 5.请补全代码: x=eval(input(\请输入小于1000的整数:\)) k=2 whilex>1: if: print(k,end=\) x = x//k if x >1: print(\,end=\) else: 答案:print(x,\,end=\),x % k==0 ,k += 1 5. 验证哥德巴赫猜想:一个大于等于6的偶数可以表示为两个素数之和,请补全代码。 import math x=eval(input(“输入一个数”)) while x<6 or x%2==1: #大于6的偶数 x=eval(input(“输入一个数”)) for n1 in : m1=int(math.sqrt(n1)+1) for iin range(2,m1): #2-sqrt(n1) if : break else: m2=math.ceil(math.sqrt(n2)+1) for j in range(2,m2): if n2%j==0 and j else: print(x,'=',n1,'+',n2) 答案:range(3,x//2,2),n1%i==0,n2=x-n1, break 6. 右对齐九九乘法表,请补全代码: for iin range(1,10): s1=str(i)+'×'+str(j)+'='+str(i*j) print() print() 答案:for j in range(1,i+1):,'%8s'%(s1),end='' 7. 输出右边图形,请补充程序: fori: print((‘ *’*(2*i-1)).center(30)) for i in range(6, 0, -1): print() 答案:in range(6);(' *‘(2*i-1)).center(5*n) 8. 下面代码判断回文串,请补全代码: s='雾锁山头山锁雾' low=0 high= while low if : #倒序和顺序不一样 print(s,\不是回文\ low +=1 else: print(s,\是回文串\ 答案:len(s)-1,s[low]!=s[high],break,high -=1; 9. 输出右边图形,请补充程序: s='''I am a teacher! You are students!''' print('='*30) print('统计信息'.center(28)) print() item_width=25 line=s.count('\\n')+1 word=s.split() char=list(s) #包含空格、标点符号 w_num=0 for w in word: w_num+= print('%-*s ]'%(item_width-3,'行数',line)) print('%-*s ]'%(,'单词数',len(word)))