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')
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题