当前位置:   article > 正文

Python 入门编程题(二)答案_for i in range(len(lt)): #遍历你输入类型序列的长度(假设输入stirng

for i in range(len(lt)): #遍历你输入类型序列的长度(假设输入stirng 则 len(lt)

Python 入门编程题:11~20(答案)

提示:最好还是先思考,先编写,再看答案哦 ^_^

11.

  1. X = [[12,7,3],
  2. [4,5,6],
  3. [7,8,9]]
  4. Y = [[5,8,1],
  5. [6,7,3],
  6. [4,5,9]]
  7. result = [[0,0,0],
  8. [0,0,0],
  9. [0,0,0]]
  10. # 迭代输出行
  11. for i in range(len(X)):
  12. # 迭代输出列
  13. for j in range(len(X[0])):
  14. result[i][j] = X[i][j] + Y[i][j]
  15. for r in result:
  16. print(r)

12.

  1. def inp(numbers):
  2. for i in range(9):
  3. numbers.append(int(input('input a number:')))
  4. numbers.append(int(input('input a number:')))
  5. p = 0
  6. def max_min(array):
  7. max = min = 0
  8. for i in range(1,len(array) - 1):
  9. p = i
  10. if array[p] > array[max] : max = p
  11. elif array[p] < array[min] : min = p
  12. k = max
  13. l = min
  14. array[0],array[l] = array[l],array[0]
  15. array[9],array[k] = array[k],array[9]
  16. def outp(numbers):
  17. for i in range(len(numbers)):
  18. print(numbers[i])
  19. if __name__ == '__main__':
  20. array = []
  21. inp(array)
  22. max_min(array)
  23. outp(array)

13.

  1. import random,time
  2. time1=time.time()
  3. n=50
  4. k=20000000
  5. c=[]
  6. #c=[1,2,3,4,5]
  7. for i in range(n):
  8. c.append(random.randint(1,100))
  9. cs=[1 if i==0 else 0 for i in range(n+1)]
  10. ta=[]
  11. for j in range(n+1):
  12. for i in range(n-j):
  13. cs[i+1]+=cs[i]
  14. ta.append(cs[-1])
  15. del cs[-1]
  16. ta[0]+=ta[-1]
  17. del ta[-1]
  18. k_n=k//n
  19. kn=k%n
  20. for i in range(k_n):
  21. cc=c.copy()
  22. for j in range(n):
  23. xx=0
  24. for k in range(n):
  25. xx=xx+(cc[k]*ta[k])%100
  26. c[j]=xx%100
  27. cc=cc[1:]+cc[:1]
  28. for i in range(kn):
  29. c.append(c[0])
  30. for j in range(n):
  31. c[j]=(c[j]+c[j+1])%100
  32. del c[-1]
  33. print(c,time.time()-time1)

14.

  1. num = int(input('多少人围成一圈:'))
  2. list1 = list(range(1, num + 1))
  3. n = 2 % len(list1)
  4. while len(list1) > 1:
  5. list1.pop(n)
  6. n = (n + 2) % len(list1)
  7. print('最后留下的是原来第%d号'%list1[0])

15.

  1. def even_sum(n):
  2. s1 = 0
  3. for i in range(2, n+1, 2):
  4. s1 += 1.0 / i
  5. return s1
  6. def odd_sum(n):
  7. s2 = 0
  8. for i in range(1, n+1, 2):
  9. s2 += 1.0 / i
  10. return s2
  11. def my_sum(n):
  12. if n % 2 == 0:
  13. s = even_sum(n)
  14. else :
  15. s = odd_sum(n)
  16. return s
  17. if __name__ == '__main__':
  18. n = int(input('input a number:\n'))
  19. sum = my_sum(n)
  20. print(sum)

16.

  1. monkey=1#要分桃的猴子数
  2. peaches=1#桃的总数
  3. peach=1#每次分桃的总数
  4. while(monkey<=5):
  5. #如果成功分桃子
  6. if peach % 5 ==1 and peach // 5 != 0:
  7. #可分桃的总数为现在的4/5
  8. peach=(peach // 5)*4
  9. # 换下一个猴子
  10. monkey += 1
  11. # 如果失败,重新分桃这时可分桃数量加1
  12. else:
  13. peaches += 1
  14. peach = peaches
  15. monkey = 1
  16. print("桃的最小总数为:%s" %peaches)

17.

  1. def calc(list1):
  2. min = abs(list1[0] - list1[1])
  3. for i in range(len(list1)):
  4. for j in range(i+1, len(list1)):
  5. if abs(list1[i] - list1[j]) < min:
  6. min = abs(list1[i] - list1[j])
  7. return min
  8. if __name__ == "__main__":
  9. test = [1, 3, 10, 5, 23, 50]
  10. print(calc(test))

18.

  1. import re
  2. list1 = input('请输入一组数字:')
  3. list1 = re.findall(r'\d', list1)
  4. for i in range(len(list1)):
  5. list1[i] = int(list1[i])
  6. list1.reverse()
  7. for each in list1:
  8. print(each, end = ' ')

19.

  1. import os
  2. while True:
  3. filename=input('请输入文件名:')
  4. if os.path.exists(filename):
  5. print('此文件已存在!')
  6. break
  7. else:
  8. all=[]
  9. while True:
  10. content=input('请加入内容:')
  11. if content=='.':
  12. print('退出')
  13. break
  14. else:
  15. all.append(content)
  16. fobj=open(filename,'w')
  17. str_list=[i+'\n'for i in all]
  18. fobj.writelines(str_list)
  19. fobj.close()
  20. break

20.

创建游戏画布:

  1. from tkinter import *
  2. import random
  3. import time
  4. tk = Tk()
  5. tk.title("Game")
  6. tk.resizable(0,0)
  7. tk.wm_attributes("-topmost",1)
  8. canvas = Canvas(tk,width=500,height=400,bd=0,highlightthickness=0)
  9. canvas.pack()
  10. tk.update()

创建 ball 类:

  1. class Ball:
  2. def __init__(self,canvas,color):
  3. self.canvas = canvas
  4. self.id = canvas.create_oval(10,10,25,25,fill=color)
  5. self.canvas.move(self.id,245,100)
  6. def draw(self):
  7. pass

创建主循环:

  1. while 1:
  2. tk.update_idletasks()
  3. tk.update()
  4. time.sleep(0.01)

创建实例化对象:

ball = Ball(canvas,'red')

(未完待续,持续更新)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/145131
推荐阅读
相关标签
  

闽ICP备14008679号