码迷,mamicode.com
首页 > 其他好文 > 详细

Pyton 练习题2

时间:2021-06-18 19:20:19      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:return   pil   number   索引   练习   compile   pattern   turn   als   

  1 #显示跑马灯文字
  2 import os
  3 import time
  4 def main():
  5     content = str(input(输入显示内容:))
  6     while True:
  7         os.system(cls)
  8         print(content)
  9         time.sleep(0.2)
 10         content = content[1:] + content[0]
 11 main()
 12 
 13 #产生验证码
 14 import random
 15 def generate_code(code_len=4):
 16     all_char = 1234567890qwertyuiopasdfghjklmnbvcxzQWERTYUIOPLKJHGFDSAZXCVBNM
 17     longs = len(all_char) - 1
 18     code =  
 19     for i in range(code_len):
 20         index = random.randint(0,longs)
 21         code += all_char[index]
 22     return code
 23 print(generate_code())
 24 
 25 #返回文件后缀名(不好理解)
 26 def get_file(filename,dot=False):
 27     pos = filename.rfind(.)
 28     print(pos)
 29     if 0 < pos < len(filename) - 1:
 30         index = pos if dot else pos + 1
 31         return filename[index:]
 32     else:
 33         return  
 34 print(get_file(ddd.py))
 35 
 36 #列表最大的两个数
 37 def max(*lists):
 38     longs = len(lists)
 39     a = 0
 40     b = 0
 41     d=[]
 42     for i in range(0,longs):
 43         if lists[i] >=a:
 44             a =  lists[i]
 45     for f in lists:
 46         d.append(f)
 47     d.remove(a)
 48     for x in range(0,longs-1):
 49         if d[x] >=b:
 50             b =  d[x]
 51     return a,b
 52 print(max(1,4,6,7,34,66,6))
 53 
 54 #根据年月日确定是第几天
 55 def is_leap_year(year):
 56    
 57     return year % 4 == 0 and year % 100 != 0 or year % 400 == 0
 58 
 59 
 60 def which_day(year, month, date):
 61   
 62    
 63     days_of_month = [
 64         [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
 65         [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
 66     ][is_leap_year(year)]
 67     total = 0
 68     for index in range(month - 1):
 69         total += days_of_month[index]
 70     return total + date
 71 
 72 
 73 def main():
 74     print(which_day(1980, 11, 28))
 75     print(which_day(1981, 12, 31))
 76     print(which_day(2018, 1, 1))
 77     print(which_day(2016, 3, 1))
 78 
 79 
 80 if __name__ == __main__:
 81     main()
 82 
 83 #杨辉三角
 84 def yanghui():
 85     num= int(input(输入打印多少行:))
 86     yh = [[]]*num   #准备num个二阶数组
 87     for row in range(len(yh)):   #打印num行的数据
 88         yh[row] = [None] * (row + 1)   #每一行显示的个数
 89         for col in range(len(yh[row])):  #打印每一行数据 
 90             if col == 0 or col == row:     #第一列和索引相同处赋值为1
 91                 yh[row][col] = 1
 92             else:                           #其他位置的值等于左上方的值加上正上方的值
 93                 yh[row][col] = yh[row-1][col] + yh[row-1][col-1]
 94             print(yh[row][col], end = \t)   
 95         print()
 96 
 97 print(yanghui())
 98 
 99 #机选双色球
100 from random import randrange, randint, sample
101 def function2(row):
102     for a, ball in enumerate(row):  #循环索引和数值
103         if a == len(row) - 1:
104             print(|, end =  )  #最后一位和前六位区分开
105         print(%02d % ball, end= ) #打印每位数值,不够2位用0表示
106     print()
107 def function1():
108     fw = [x for x in range(1, 34)] #定义取值区间
109     qian = []
110     qian = sample(fw, 6)   #在区间内随机选取六个数
111     qian.sort()
112     qian.append(randint(1,16))  #随机选取确定最后一位数
113     return qian
114 def main():
115     number = int(input(机选几注:))
116     for _ in range(number):
117         function2(function1())
118 print(main())
119 
120 #正则表达式(提取电话号码)
121 import re
122 def main():
123     pattern = re.compile(r(?<=\D)1[34578]\d{9}(?=\D))
124     sentence = ‘‘‘
125     通过学校3450054452975次的调查,126     发现5月28日号码13784932456127     给号码15875349204发过短信17438904720
128     ‘‘‘
129     mylist = re.findall(pattern,sentence)
130     print(mylist)
131     for temp in pattern.finditer(sentence):
132         print(temp.group())
133     m = pattern.search(sentence)
134     while m:
135         print(m.group())
136 if __name__ == __main__:
137     main()

 

Pyton 练习题2

标签:return   pil   number   索引   练习   compile   pattern   turn   als   

原文地址:https://www.cnblogs.com/Uriel-w/p/14897686.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!