Welcome 微信登录

首页 / 操作系统 / Linux / 安装Python的visual模块时报错

买了本学习python的书,看了几天,想写个简单的程序,下面就是一个简单的类似记事本一样的小程序,器功能主要有:1提示你是想流泪这个文件还是想写东西到这个文件2.输入R或者r,表示想看这个程序写的文件3,输入W或者w,表示想写东西到这个文件4,如果输入Q或者q,表示想退出程序期间得到很到热心的Q友的帮助,下面贴出代码和注释,大家感兴趣可以看下,有好多地方可以修改的地方,如果有不对的地方,希望大家提出宝贵意见 
  1. #!/usr/bin/env python 
  2. import os 
  3. import string 
  4. print ("This notepad write by python 2.6") 
  5. print ("Editor: Iding") 
  6. print ("Version:0.0.1") 
  7.  
  8.  ##这是判断取得昵称的函数,有判断在里面,判断输入昵称的长度
  9. def input_nick(): 
  10.         while True: 
  11.                 nick=raw_input("please input your nick:") 
  12.                 if len(nick)<3: 
  13.                         print ("your nick too short ,please again!") 
  14.                 elif len(nick)>15: 
  15.                         print ("your nick too long ,please again!") 
  16.                 else: 
  17.                         print ("Your nick is %s" % nick) 
  18.             return nick 
  19.                         break 
  20. ##这是判断取得手机号码的函数,有判断在里面,判断输入号码的长度和类型
  21. def input_mob_number(): 
  22.         while True: 
  23.                 mob_number=raw_input("please input your mob_number:") 
  24.                 if len(mob_number) !=11 :  #长度必须是11位
  25.                         print ("Your mob_number"s length is wrong ,please again!") 
  26.                 elif mob_number.isdigit() and len(mob_number)==11 : 
  27. #长度必须是11位且必须都是数字
  28.                         print ("Your mob_number is %s" % mob_number) 
  29.             return mob_number 
  30.                         break 
  31.                 else : 
  32.                         print ("Your input has wrong charter,please again!") 
  33. ##这是判断取得QQ号的函数,有判断在里面,判断输入昵称的长度和类型
  34. def input_qq(): 
  35.         while True: 
  36.                 qq=raw_input("please input your QQ_Number:") 
  37.                 if len(qq) <6 : 
  38. #号码长度必须大于6位
  39.                         print ("your number is too short!") 
  40.                 elif qq.isdigit() and len(qq) <=12 : 
  41. #号码必须都是数字且小于12位
  42.                         print ("Your qq number is: %s" % qq ) 
  43.             return qq 
  44.                         break 
  45.                 else: 
  46.                         print ("you input has wrong character!") 
  47. while True: 
  48.         print ("#############This is a notepad programe writed by python!################") 
  49.         print ("#############please input your choice:R or W#############################") 
  50.         print ("#############if you input r or R ,mean you want to read notepad##########") 
  51.         print ("#############if you input w or W mean you want to write to notepad#######") 
  52.         print ("#############if you input q or Q mean your want to quit #################") 
  53.         print ("") 
  54.     print ("") 
  55.     print ("") 
  56.         input=raw_input("please input your choice:") 
  57.  
  58.         if input.startswith("r") or input.startswith("R"): 
  59.         print ("") 
  60.                 print ("you want to read file") 
  61.         print ("") 
  62.         print ("")           
  63.        f = open("notepad.txt")  #打开文件
  64.          totallines=len(f.readlines())  #得到文件总的行数
  65.          print ("This notepad has  %s records" % totallines ) #说明共有几行内容
  66.          f.close() #关闭文件,
  67.          f1=open("notepad.txt")   #这里又要打开文件,这里比较纠结,应该有更好的方法
  68.          for line_number in range(1,totallines+1): 
  69.              content=f1.readline() 
  70.              print "NO. "+ str(line_number)+" :  "+ content 
  71. #打印文件内容
  72.   f1.close()
  73.                 break 
  74.         elif input.startswith("w") or input.startswith("W"): 
  75.             print ("you want to write file") 
  76.             nick=input_nick() #得到昵称
  77.         
  78.             mob_number=input_mob_number()  #得到号码
  79.             qq=input_qq() 
  80.         
  81.             notepad=file("notepad.txt","a")  #追加方式打开文件
  82.             print >>notepad,nick,mob_number,qq  #把内容写入文件
  83.             notepad.close()  
  84.                 
  85.         elif input.startswith("q") or input.startswith("Q"): 
  86.                 print ("you want to exit programe") 
  87.         break 
  88.         else: 
  89.             print ("your input has wrong character,please again!") 
  90.        

上面的代码是比较粗糙的,有好多地方需要修改,尤其是对文件的操作,不是很清楚,以至于要2次打开和关闭文件,希望有人可以告诉我如何修改,只要一次打开i文件就可以完成操作,这个程序没有涉及到吧数据写入数据库,下次准备改下,把内容写入到数据库中。由于python对源代码的格式缩进有严格的要求,所有大家写的时候要注意,我把源代码也传了上来,可以看看。免费下载地址在 http://linux.linuxidc.com/用户名与密码都是www.linuxidc.com具体下载目录在 /pub/2011/11/21/安装Python的visual模块时报错/