设为首页 收藏本站
查看: 859|回复: 0

[经验分享] python学习之记事本

[复制链接]

尚未签到

发表于 2018-8-6 13:10:14 | 显示全部楼层 |阅读模式
  买了本学习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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-547679-1-1.html 上篇帖子: python函数的参数 下篇帖子: python之nntp服务器组
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表