歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 安裝Python的visual模塊時報錯

安裝Python的visual模塊時報錯

日期:2017/3/1 10:57:02   编辑:Linux編程
買了本學習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. def input_nick():
  9. while True:
  10. nick=raw_input("please input your nick:")
  11. if len(nick)<3:
  12. print ("your nick too short ,please again!")
  13. elif len(nick)>15:
  14. print ("your nick too long ,please again!")
  15. else:
  16. print ("Your nick is %s" % nick)
  17. return nick
  18. break
  19. ##這是判斷取得手機號碼的函數,有判斷在裡面,判斷輸入號碼的長度和類型
  20. def input_mob_number():
  21. while True:
  22. mob_number=raw_input("please input your mob_number:")
  23. if len(mob_number) !=11 : #長度必須是11位
  24. print ("Your mob_number's length is wrong ,please again!")
  25. elif mob_number.isdigit() and len(mob_number)==11 :
  26. #長度必須是11位且必須都是數字
  27. print ("Your mob_number is %s" % mob_number)
  28. return mob_number
  29. break
  30. else :
  31. print ("Your input has wrong charter,please again!")
  32. ##這是判斷取得QQ號的函數,有判斷在裡面,判斷輸入昵稱的長度和類型
  33. def input_qq():
  34. while True:
  35. qq=raw_input("please input your QQ_Number:")
  36. if len(qq) <6 :
  37. #號碼長度必須大於6位
  38. print ("your number is too short!")
  39. elif qq.isdigit() and len(qq) <=12 :
  40. #號碼必須都是數字且小於12位
  41. print ("Your qq number is: %s" % qq )
  42. return qq
  43. break
  44. else:
  45. print ("you input has wrong character!")
  46. while True:
  47. print ("#############This is a notepad programe writed by python!################")
  48. print ("#############please input your choice:R or W#############################")
  49. print ("#############if you input r or R ,mean you want to read notepad##########")
  50. print ("#############if you input w or W mean you want to write to notepad#######")
  51. print ("#############if you input q or Q mean your want to quit #################")
  52. print ("")
  53. print ("")
  54. print ("")
  55. input=raw_input("please input your choice:")
  56. if input.startswith('r') or input.startswith('R'):
  57. print ("")
  58. print ("you want to read file")
  59. print ("")
  60. print ("")
  61. f = open('notepad.txt') #打開文件
  62. totallines=len(f.readlines()) #得到文件總的行數
  63. print ("This notepad has %s records" % totallines ) #說明共有幾行內容
  64. f.close() #關閉文件,
  65. f1=open("notepad.txt") #這裡又要打開文件,這裡比較糾結,應該有更好的方法
  66. for line_number in range(1,totallines+1):
  67. content=f1.readline()
  68. print "NO. "+ str(line_number)+" : "+ content
  69. #打印文件內容
  70. f1.close()
  71. break
  72. elif input.startswith('w') or input.startswith('W'):
  73. print ("you want to write file")
  74. nick=input_nick() #得到昵稱
  75. mob_number=input_mob_number() #得到號碼
  76. qq=input_qq()
  77. notepad=file("notepad.txt","a") #追加方式打開文件
  78. print >>notepad,nick,mob_number,qq #把內容寫入文件
  79. notepad.close()
  80. elif input.startswith('q') or input.startswith('Q'):
  81. print ("you want to exit programe")
  82. break
  83. else:
  84. print ("your input has wrong character,please again!")

上面的代碼是比較粗糙的,有好多地方需要修改,尤其是對文件的操作,不是很清楚,以至於要2次打開和關閉文件,希望有人可以告訴我如何修改,只要一次打開i文件就可以完成操作,這個程序沒有涉及到吧數據寫入數據庫,下次准備改下,把內容寫入到數據庫中。

由於python對源代碼的格式縮進有嚴格的要求,所有大家寫的時候要注意,我把源代碼也傳了上來,可以看看。

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /pub/2011/11/21/安裝Python的visual模塊時報錯/

Copyright © Linux教程網 All Rights Reserved