在python學的半入門不入門的時候,決定寫個東西把python的最基本的東西鞏固一下。python也是一個既支持OO又支持面向過程結構化編程的腳本語言,前者思想類似Java,後者思想類似C,因此比較容易上手,上手後你會發現python確實非常強大,也非常有用。
記得第一次有想學python是剛去lab時,當時第一個任務是從網頁上把數據扒下來。當時cliff強烈建議我用python實現從網頁上獲取數據,但是當時確實一點python的基礎都沒有,我還是用Java的網絡編程的相關內容+Java的正則表達式實現了數據的獲取。後來那個Web Service的測試對方給的測試腳本也是用python寫的,當時因爲不懂python受盡了同僚師兄的鄙視,所以趁着有時間趕緊補補python,有個基礎就行。
個人感覺python讓我眼前一亮的特點就是變量從來不用聲明,其實嚴格來說也不叫不用聲明,就是賦值時即爲聲明,而且賦值時不用顯示說明變量是什麼類型。而且,python裏面代碼組不用“{ }”,而是靠嚴格的縮進來判斷代碼組。注意這個縮進是非常嚴格的,可能因爲你的代碼多了一個空格編譯就報錯,python的這個特點也建議我們們在寫python代碼時最好不用Tab鍵而多敲幾次空格鍵,因爲不同的文本編輯器對Tab等於幾個空格的設置是不一樣的。
在寫這個東西時,感觸很大的就是不要因爲python聲明變量時不顯示指明類型而在你的代碼中就可以隨意混淆類型,普通整形永遠都是普通整形,字符串類型永遠是字符串類型,必要的時候必須轉換一下,下面上代碼,有bug歡迎指正:
# -*- coding: cp936 -*-
class Person(object) :
'''用戶和管理員的基類'''
def __init__(self,ID,password,startDate) :
self.ID = ID
self.oldPassword = self.password = password
self.startDate = startDate
#self.shouldSave = False
def changePassword(self) :
'''修改密碼'''
while True :
old_password = raw_input("請輸入舊密碼:")
if old_password == self.password :
break
else :
print "舊密碼不正確!"
return None
if self.checkPassword() == True :
print "密碼修改成功!"
return None
def checkPassword(self) :
while True :
while True :
new_password = raw_input("請輸入新密碼:")
if len(new_password) < 6 :
print "密碼長度至少爲6位,請重新輸入!"
continue
flag = False
for c in new_password :
if c != new_password[0] :
flag = True
break
if not flag :
print "密碼不能各位全相同,請重新輸入!"
else :
break
new_password_again = raw_input("請再次輸入新密碼:")
if new_password == new_password_again :
self.password = new_password
return True
else :
print "兩次輸入密碼不一致,請重新輸入新密碼!"
# -*- coding: cp936 -*-
import sys,time
from PersonPackage import Person
class Admin(Person) :
'''管理員類,繼承自Person類'''
def __init__(self,ID,password,startDate) :
Person.__init__(self,ID,password,startDate)
def inquireHq(self) :
info_file = open(r"D:/UserInfo.txt","r+")
find_flag = False
for eachLine in info_file :
info_list = eachLine.strip().split("#")
if len(info_list) < 5 : # 每次寫入文件都會加上換行符,因此最後一行是一個回車,加一個判斷判斷是不是回車即可
break
if info_list[3] == "HQ" :
find_flag = True
print "用戶ID:" + info_list[0] + "\t",
print "賬戶餘額:" + info_list[2] + "\t",
print "開戶時間:" + info_list[4]
if find_flag == False :
print "沒有找到任何用戶的信息!"
info_file.close()
def createNewUser(self) :
newID = raw_input("請輸入新賬號:")
while True :
while True :
new_password = raw_input("請輸入新密碼:")
if len(new_password) < 6 :
print "密碼長度至少爲6位,請重新輸入!"
continue
flag = False
for c in new_password :
if c != new_password[0] :
flag = True
break
if not flag :
print "密碼不能各位全相同,請重新輸入!"
else :
break
new_password_again = raw_input("請再次輸入新密碼:")
if new_password == new_password_again :
newPassword = new_password
break
else :
print "兩次輸入密碼不一致,請重新輸入新密碼!"
while True :
newRemains = int(raw_input("請輸入初始金額:"))
if newRemains % 100 != 0 :
print "賬戶金額必須爲100的整數倍!"
else :
break
newInfo = newID + "#" + new_password + "#" + str(newRemains) + "#" + "HQ" + "#" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "\n"
info_file = info_file = open(r"D:/UserInfo.txt","a")
info_file.write(newInfo)
info_file.close()
print "新賬戶創建完畢!"
def inquireATMRemains(self) :
info_file = open(r"D:/UserInfo.txt","r+")
Sum = 0
for eachLine in info_file :
info_list = eachLine.strip().split("#")
Sum += int(info_list[2])
print "當前ATM的餘額爲" + str(Sum) + "元."
info_file.close()
def savePersonFile(self) :
'''把個人信息保存到信息文件中'''
#if shouldSave == True :
info_file = open(r"D://UserInfo.txt","r+")
info_file_all = []
for eachLine in info_file :
info_list = eachLine.strip().split("#")
if self.ID == info_list[0] and self.oldPassword == info_list[1] :
newInfo = self.ID + "#" + self.password + "#" + "0" + "#" + "ADMIN" + "#" + self.startDate
info_file_all.append(newInfo+"\n")
else :
info_file_all.append(eachLine)
info_file.close()
info_file = open(r"D://UserInfo.txt","w")
for eachItem in info_file_all :
info_file.write(eachItem)
info_file.close()
# -*- coding: cp936 -*-
import time
from PersonPackage import Person
class User(Person) :
'''活期用戶類,繼承自Person類'''
def __init__(self,ID,password,remains,startDate) :
Person.__init__(self,ID,password,startDate)
self.remains = int(remains)
#self.startDate = startDate
def inquireRemains(self) :
'''當前用戶查詢餘額'''
print "您的餘額爲"+str(self.remains)+"元~"
def putMoney(self) :
'''存款'''
while True :
toPut = int(raw_input("請輸入您的存款金額:"))
if (toPut % 100) != 0 :
print "每次存款金額必須爲100的整數倍!"
continue
if (toPut > 2000) :
print "每次存款的金額最多爲2000元!"
continue
break
self.remains += toPut
self.shouldSave = True
record = self.ID + "#" + str(toPut) + "#" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "\n"
record_file = open(r"D://Record.txt","a")
record_file.write(record)
record_file.close()
print "存款成功!"
def getMoney(self) :
'''取款'''
while True :
toGet = int(raw_input("請輸入您的取款金額:"))
if toGet > self.remains :
print "您的賬戶餘額不足!"
continue
if (toGet % 100) != 0 :
print "每次取款金額必須爲100的整數倍!"
continue
if (toGet > 2000) :
print "每次取款的金額最多爲2000元!"
continue
break
self.remains -= toGet
self.shouldSave = True
record = self.ID + "#" + "-" + str(toGet) + "#" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "\n"
record_file = open(r"D://Record.txt","a")
record_file.write(record)
record_file.close()
print "取款成功!"
def inquireDealingDetails(self) :
'''查詢交易明細'''
record_file = open(r"D://Record.txt","r+")
find_flag = False
for eachLine in record_file :
record_list = eachLine.strip().split("#")
if self.ID == record_list[0] :
find_flag = True
if int(record_list[1]) < 0 :
print "支出",abs(int(record_list[1])),"元",
print "\t日期:"+record_list[2]
else :
print "存入",int(record_list[1]),"元",
print "\t日期:"+record_list[2]
if find_flag == False :
print "未找到與您相關的交易記錄!"
record_file.close()
def savePersonFile(self) :
'''把個人信息保存到信息文件中'''
#if shouldSave == True :
#self.displayInfo()
info_file = open(r"D://UserInfo.txt","r+")
info_file_all = []
for eachLine in info_file :
info_list = eachLine.strip().split("#")
if self.ID == info_list[0] and self.oldPassword == info_list[1] :
newInfo = self.ID + "#" + self.password + "#" + str(self.remains) + "#" + "HQ" + "#" + self.startDate
info_file_all.append(newInfo+"\n")
else :
info_file_all.append(eachLine)
info_file.close()
info_file = open(r"D://UserInfo.txt","w")
for eachItem in info_file_all :
info_file.write(eachItem)
info_file.close()
def displayInfo(self) :
print "-"*80
print "self.ID",self.ID
print "self.password",self.password
print "self.remains",self.remains
print "self.startDate",self.startDate
print "-"*80
# -*- coding: cp936 -*-
'''這個模塊作爲主模塊執行'''
import sys
from AdminPackage import Admin
from UserPackage import User
while True:
print "請輸入您的卡號和密碼,如果賬號和密碼其一爲\"0000\",則程序退出~"
temp_ID = raw_input("請輸入卡號:")
if temp_ID == "0000":
sys.exit()
temp_password = raw_input("請輸入密碼:")
if temp_password == "0000":
sys.exit()
fp = open(r"D:/UserInfo.txt","r+")
find_flag = 0 #初值爲0
for eachLine in fp :
info_list = eachLine.strip().split("#")
if temp_ID == info_list[0] and temp_password == info_list[1]:
if info_list[3] == "HQ":
find_flag = 1 #說明找到的是活期用戶
currentUser = User(info_list[0],info_list[1],info_list[2],info_list[4])
fp.close()
break
elif info_list[3] == "ADMIN":
find_flag = 2 #說明找到的是管理員
currentUser = Admin(info_list[0],info_list[1],info_list[4])
fp.close()
break
if find_flag == 0:
fp.close()
print "*" * 50
print "未找到您的信息,請重新輸入~"
print "*" * 50
else:
break
while True :
if find_flag == 1:
print "歡迎你,用戶!"
print "*" * 20
print "1.查詢餘額"
print "2.存款"
print "3.取款"
print "4.查詢存取款明細"
print "5.修改密碼"
print "0.退出"
print "*" * 20
choice = raw_input("請輸入您的選擇:")
if choice == "1" :
currentUser.inquireRemains()
elif choice == "2" :
currentUser.putMoney()
currentUser.savePersonFile()
elif choice == "3" :
currentUser.getMoney()
currentUser.savePersonFile()
elif choice == "4" :
currentUser.inquireDealingDetails()
elif choice == "5" :
currentUser.changePassword()
currentUser.savePersonFile()
else :
currentUser.savePersonFile()
break
elif find_flag == 2:
print "歡迎你,管理員!"
print "*" * 20
print "1.查詢所有活期用戶信息"
print "2.創建活期賬號(開新卡)"
print "3.查詢當前ATM資金餘額"
print "4.修改密碼"
print "0.退出"
print "*" * 20
choice = raw_input("請輸入您的選擇:")
if choice == "1" :
currentUser.inquireHq()
elif choice == "2" :
currentUser.createNewUser()
elif choice == "3" :
currentUser.inquireATMRemains()
elif choice == "4" :
currentUser.changePassword()
currentUser.savePersonFile()
else :
currentUser.savePersonFile()
break
else :
print "系統出現錯誤~~"
sys.exit()