clh899 发表于 2018-8-6 13:10:14

python学习之记事本

  买了本学习python的书,看了几天,想写个简单的程序,下面就是一个简单的类似记事本一样的小程序,器功能主要有:
  1提示你是想流泪这个文件还是想写东西到这个文件
  2.输入R或者r,表示想看这个程序写的文件
  3,输入W或者w,表示想写东西到这个文件
  4,如果输入Q或者q,表示想退出程序
  期间得到很到热心的Q友的帮助,下面贴出代码和注释,大家感兴趣可以看下,有好多地方可以修改的地方,如果有不对的地方,希望大家提出宝贵意见
  


[*]#!/usr/bin/env python
[*]import os
[*]import string
[*]print ("This notepad write by python 2.6")
[*]print ("Editor: Iding")
[*]print ("Version:0.0.1")
[*]
[*] ##这是判断取得昵称的函数,有判断在里面,判断输入昵称的长度
[*]def input_nick():
[*]      while True:
[*]                nick=raw_input("please input your nick:")
[*]                if len(nick)<3:
[*]                        print (&quot;your nick too short ,please again!&quot;)
[*]                elif len(nick)>15:
[*]                        print (&quot;your nick too long ,please again!&quot;)
[*]                else:
[*]                        print (&quot;Your nick is %s&quot; % nick)
[*]            return nick
[*]                        break
[*]##这是判断取得手机号码的函数,有判断在里面,判断输入号码的长度和类型
[*]def input_mob_number():
[*]      while True:
[*]                mob_number=raw_input(&quot;please input your mob_number:&quot;)
[*]                if len(mob_number) !=11 :#长度必须是11位
[*]                        print (&quot;Your mob_number's length is wrong ,please again!&quot;)
[*]                elif mob_number.isdigit() and len(mob_number)==11 :
[*]#长度必须是11位且必须都是数字
[*]                        print (&quot;Your mob_number is %s&quot; % mob_number)
[*]            return mob_number
[*]                        break
[*]                else :
[*]                        print (&quot;Your input has wrong charter,please again!&quot;)
[*]##这是判断取得QQ号的函数,有判断在里面,判断输入昵称的长度和类型
[*]def input_qq():
[*]      while True:
[*]                qq=raw_input(&quot;please input your QQ_Number:&quot;)
[*]                if len(qq) <6 :
[*]#号码长度必须大于6位
[*]                        print (&quot;your number is too short!&quot;)
[*]                elif qq.isdigit() and len(qq) <=12 :
[*]#号码必须都是数字且小于12位
[*]                        print (&quot;Your qq number is: %s&quot; % qq )
[*]            return qq
[*]                        break
[*]                else:
[*]                        print (&quot;you input has wrong character!&quot;)
[*]while True:
[*]      print (&quot;#############This is a notepad programe writed by python!################&quot;)
[*]      print (&quot;#############please input your choice:R or W#############################&quot;)
[*]      print (&quot;#############if you input r or R ,mean you want to read notepad##########&quot;)
[*]      print (&quot;#############if you input w or W mean you want to write to notepad#######&quot;)
[*]      print (&quot;#############if you input q or Q mean your want to quit #################&quot;)
[*]      print (&quot;&quot;)
[*]    print (&quot;&quot;)
[*]    print (&quot;&quot;)
[*]      input=raw_input(&quot;please input your choice:&quot;)
[*]
[*]      if input.startswith('r') or input.startswith('R'):
[*]                print (&quot;&quot;)
[*]                print (&quot;you want to read file&quot;)
[*]                print (&quot;&quot;)
[*]                print (&quot;&quot;)
[*]               f = open('notepad.txt')                  #打开文件
[*]               totallines=len(f.readlines())            #得到文件总的行数
[*]               print (&quot;This notepad has%s records&quot; % totallines )   #说明共有几行内容
[*]               f.close()   #关闭文件,
[*]               f1=open(&quot;notepad.txt&quot;)   #这里又要打开文件,这里比较纠结,应该有更好的方法
[*]               for line_number in range(1,totallines+1):
[*]                   content=f1.readline()
[*]                   print &quot;NO. &quot;+ str(line_number)+&quot; :&quot;+ content
[*]#打印文件内容
[*]                f1.close()
[*]                break
[*]      elif input.startswith('w') or input.startswith('W'):
[*]            print (&quot;you want to write file&quot;)
[*]            nick=input_nick()   #得到昵称
[*]
[*]            mob_number=input_mob_number()    #得到号码
[*]            qq=input_qq()
[*]
[*]            notepad=file(&quot;notepad.txt&quot;,&quot;a&quot;)    #追加方式打开文件
[*]            print >>notepad,nick,mob_number,qq    #把内容写入文件
[*]            notepad.close()
[*]
[*]      elif input.startswith('q') or input.startswith('Q'):
[*]                print (&quot;you want to exit programe&quot;)
[*]      break
[*]      else:
[*]            print (&quot;your input has wrong character,please again!&quot;)
[*]

  

  上面的代码是比较粗糙的,有好多地方需要修改,尤其是对文件的操作,不是很清楚,以至于要2次打开和关闭文件,希望有人可以告诉我如何修改,只要一次打开i文件就可以完成操作,这个程序没有涉及到吧数据写入数据库,下次准备改下,把内容写入到数据库中。。。
  由于python对源代码的格式缩进有严格的要求,所有大家写的时候要注意,我把源代码也传了上来,如果感谢去,可以看看。。。。
页: [1]
查看完整版本: python学习之记事本