python小练习,打出1-100之间的所有偶数,设计一个函数,在桌面上创建10个文件,并以数字命名,复利计算函数

  练习一:打出1-100之间的所有偶数
1
2
3
4
5
def even_print():
    for i in range(1,101):
        if i % 2 == 0:
            print (i)
even_print()
1
2
3
#列表解析式的方式:
k = [n for n in range(1,101) if n%2 == 0]
print (k)

  

练习二:设计一个函数,在桌面上创建10个文件,并以数字命名

1
2
3
4
5
6
7
8
def text_creation():
    path = 'C:/Users/Administrator/Desktop'
    for name in range (1,11):
        with open(path + str(name) + '.txt','w') as text:
            text.write(str(name))
            text.close()
            print ('done')
text_creation()

 练习三:复利计算函数

1
2
3
4
5
6
7
def invest(amount,rate,time):
    print('principal amount:{}'.format(amount))
    for t in range(1,time + 1):
        amount = amount * (1 + rate)
        print ('year {}: ${}'.format(t,amount))
invest(100,.05,8)
invest(2000,.025,5)

 练习四:随机验证码:

1
2
3
4
5
6
7
8
9
10
import random
checkcode = ''
for i in range(4):
    current = random.randrange(0,4)
    if current != i:
        temp = chr(random.randint(65,90))
    else:
        temp = random.randint(0,9)
    checkcode += str(temp)
print (checkcode)

  执行结果:

1
2
C:\Users\Administrator\AppData\Local\Programs\Python\Python35\python.exe D:/python3/shenqiankaobei.py
22U3

  

posted @   BigZero  阅读(9799)  评论(0编辑  收藏  举报
编辑推荐:
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
阅读排行:
· 官方的 MCP C# SDK:csharp-sdk
· 一款 .NET 开源、功能强大的远程连接管理工具,支持 RDP、VNC、SSH 等多种主流协议!
· 提示词工程师自白:我如何用一个技巧解放自己的生产力
· 一文搞懂MCP协议与Function Call的区别
· 如何不购买域名在云服务器上搭建HTTPS服务
点击右上角即可分享
微信分享提示