算法小练习

'''
题目100:列表转换为字典。
'''
def tm100():
    l = ['ak17','b51','b52','#64']
    d = {}
    for i in range(len(l)):
        d[i]=l[i]
    print(d) 

def tm100_1():
    # 用zip函数更简单
    l = ['ak17','b51','b52','#64']
    print(dict(zip(range(4),l)))
tm100_1()
'''
题目099:有两个磁盘文件A和B,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列), 输出到一个新文件C中。
'''
def tm099():
    '''
    【个人备注】:读写文件
    '''
    with open('d:/a.txt','r+') as f:a=f.read()
    with open('d:/b.txt','r+') as f:b=f.read()
    with open('d:/c.txt','w+') as f:f.write(a+b)
'''
题目098:从键盘输入一个字符串,将小写字母全部转换成大写字母,然后输出到一个磁盘文件"test"中保存。
'''
import os
def tm098():
    '''
    字符串大写
    '''
    c = input()
    c = c.upper()
    try:
        with open("test.txt",mode='r',encoding='utf-8') as ff:
            print(ff.readlines())
    except FileNotFoundError:
        os.mknod('test.txt')
        print("文件创建成功!")
    with open('test.txt','w+') as f:f.write(c)
tm098()
'''
题目097:从键盘输入一些字符,逐个把它们写到磁盘文件上,直到输入一个 # 为止。
'''
def tm097():
    '''
    保存文件的方法,记住即可。
    with .. as ..打开会自动关闭。
    其他方式打开,别忘了通过代码关闭。
    '''
    path = 'test.txt'
    with open(path,'w+') as f:f.write('')
    while 1:
        c = input()
        if c=='#':
            break
        else:
            with open(path,'a+') as f:f.write(c)
tm097()
'''
题目094:时间函数举例4,一个猜数游戏,判断一个人反应快慢。
'''
def tm094():
    import time,random
    print('《猜大小0-1000之间》')
    x = random.randint(0,1000)
    flag = input('是否开始(y/n):')
    if flag=='y':
        s = time.time()
        while 1:
            m = int(input('请输入数字:'))
            if m>x:
                print('大了')
            elif m<x:
                print('小了')
            else:
                print('bingo!')
                break
        e = time.time()
        print('耗时%.2f秒'%(e-s))
        print(time.sleep(5))
# tm094()

'''
题目088:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
'''
def tm088():
    # 没啥说的
    for i in [1,4,5,14,22]:
        print('*'*i)
tm088()
  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值