1. 编写程序,创建一个求圆的面积和球体积的模块AreaVolume.py,并编写只有独立运行时才能执行的代码,要求输入半径,输出结果保留两位小数
     1 import math
     2 def area(r):
     3     return math.pi * r * r
     4 
     5 
     6 def volume(r):
     7     return 4 / 3 * math.pi * r ** 3  # 球形的体积公式为:V球=4/3πR³
     8 
     9 
    10 import AreaVolume
    11 
    12 if __name__ == '__main__':
    13     r = float(input("请输入半径:"))
    14     print("圆的面积:{0:.2f},球的体积:{1:.2f}". \
    15           format(AreaVolume.area(r), AreaVolume.volume(r)))

    输出结果:

    请输入半径:>? 5
    圆的面积:78.54,球的体积:523.60

     

  2. 写一个6位随机验证码程序(使用random模块),要求验证码中至少包含一个数字、一个小写字母、一个大写字母。
    import random
    import string
    
    src_list = []
    src_list.append(str(random.randint(1, 9)))  # 生成一个数字
    src_list.append(random.sample(string.ascii_lowercase, 1)[0])  # 生成一个小写字母
    src_list.append(random.sample(string.ascii_uppercase, 1)[0])  # 生成一个大写字母
    three = random.sample(string.digits + string.ascii_lowercase + string.ascii_uppercase, 3)  # 生成3个由大小写字母和数字组成的列表
    for i in three:
        src_list.append(i)  # 所有数加到src_list中
    random.shuffle(src_list)  # 把组合好的列表打乱
    result = ''.join(src_list)
    print(result)
    
    输出结果:
    Wi43WH

     

  3. 编写程序,实现一个根据可根据指定文本文件和图片文件生成不同形状词云的程序。

     1 import matplotlib.pyplot as plt
     2 
     3 import jieba
     4 
     5 from wordcloud import WordCloud
     6 
     7 # 1.读入txt文本数据
     8 text = open (r'test.txt', "r").read ()
     9 # print(text)
    10 # 2.结巴中文分词,生成字符串,默认精确模式,如果不通过分词,无法直接生成正确的中文词云
    11 cut_text = jieba.cut (text)
    12 # print(type(cut_text))
    13 # 必须给个符号分隔开分词结果来形成字符串,否则不能绘制词云
    14 result = " ".join(cut_text)
    15 # print(result)
    16 # 3.生成词云图,这里需要注意的是WordCloud默认不支持中文,所以这里需已下载好的中文字库
    17 wc = WordCloud (
    18     # 设置字体,不指定就会出现乱码
    19 
    20     # 设置背景色
    21 
    22     background_color='pink',
    23 
    24     # 设置背景宽
    25 
    26     width=800,
    27 
    28     # 设置背景高
    29 
    30     height=600,
    31 
    32     # 最大字体
    33 
    34     max_font_size=100,
    35 
    36     # 最小字体
    37 
    38     min_font_size=20,
    39 
    40     mode='RGBA'
    41 
    42     # colormap='pink'
    43 
    44 )
    45 
    46 # 产生词云
    47 wc.generate (result)
    48 # 保存图片
    49 wc.to_file (r"wordcloud.png")  # 按照设置的像素宽高度保存绘制好的词云图,比下面程序显示更清晰
    50 # 4.显示图片
    51 # 指定所绘图名称
    52 plt.figure ("词云picture")
    53 # 以图片的形式显示词云
    54 plt.imshow (wc)
    55 # 关闭图像坐标系
    56 plt.axis ("off")
    57 plt.show()

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
            
 
test.txt文本内容如下:

python python3 is good well bestbast shell cool
Age has reached the end of the beginning of a word. May be guilty in his seems to passing a lot of different life became the appearance of the
same day; May be backto oneself the paranoid weird belief disillusionment, these days, my mind has been very messy, in my mind constantly. Always
feel oneself should go to do something, or write something. Twenty years of life trajectory deeply shallow, suddenly feel something, do it.The end
of our life, and can meet many things really do?During myhood, think lucky money and new clothes are necessary for New Year, but as the advance of
the age, will be more and more found that those things are optional; Junior high school, thought to have a crush on just means that the real growth,
but over the past three years later, his writing of alumni in peace, suddenly found that isn't really grow up, it seems is not so important; Then
in high school, think don't want to give vent to out your inner voice can be in the high school children of the feelings in a period, but was event
ually infarction when graduation party in the throat, later again stood on the pitch he has sweat profusely, looked at his thrown a basketball hoops
, suddenly found himself has already can't remember his appearance.