Shadow丶S 2021-01-24 18:46 采纳率: 75%
浏览 25
已结题

四个python基础题目,求助,感谢

 

  • 写回答

1条回答 默认 最新

  • 皛心 2021-01-24 22:34
    关注

    第一题和第二题:

    def count(n):
        try:
            n = int(n)
        except ValueError:
            raise '请输入大于0的整数!'
        if n <= 0:
            raise '请输入大于0的整数!'
        else:
            result = 0
            for i in range(1,n+1):
                result += 2*i-1
        return result
    
    print(count(input('请输入大于0的整数:')))
    

    第三题:

    def add_record():
        table = []
        with open('3_3.txt', mode='rt') as f:
            for i in f:
                table.append(i)
            table.append(
                '\n'+'url:https://www.google.com/,e-mail:maker@163.com,usernamfor:maker')
            ret = ''
            for i in table:
                ret += i
        with open('3_3.txt', mode='wt') as f:
            f.write(ret)
    
    
    def display_record():
        dic = []
        with open('3_3.txt', mode='rt') as f:
            for i in f:
                temp = {}
                a = i.split(',')
                for i in a:
                    b = i.split(':',1)
                    temp[b[0]] = b[1]
                dic.append(temp)
        print(dic)
    
    add_record()
    display_record()
    

    第四题:

    def time_to_file(file_name):
        import time
        now = time.strftime('%Y年%m月%d日%H时%M分%S秒')
        with open(file_name,'at') as f:
            f.write(now+'\n')
    
    time_to_file('time.txt')
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?