標籤:python
環境:python2.7
缺陷:錢不變,待完善
# -*- coding: utf-8 -*-user_name = "C:\Users\95112\Desktop\ATM\username" #定義使用者名稱和密碼的位置goods = "C:\Users\95112\Desktop\ATM\goods" #定義商品列表的的位置#登入def login():global aglobal salaryusername=[]password=[]money =[]f = file(user_name)for line in f.readlines():new_line = line.split()username.append(int(new_line[0]))password.append(int(new_line[1]))money.append(int(new_line[2])) UserName = int(raw_input("please input your username:"))if UserName in username:PassWord = int(raw_input("please input your password:"))username_passwd = password[username.index(UserName)] #取出username相對應的密碼salary = money[username.index(UserName)] #取出賬戶中相對應的錢if PassWord == username_passwd:a = 1 #登入成功的標誌print "Login successful"print "You still have %s of the balance" % moneyelse:a = 0print "password error"else:a = 0print "your username error"return areturn salary#購物def shopping(salary):global sproducts=[]price=[]shop_list= []f = file(goods)for line in f.readlines():new_line = line.split()products.append(new_line[0])price.append(int(new_line[1]))while 1:print u‘請從以下商品中挑選一個或者幾個購買:‘print productsfor i in range(0,len(products)):if (salary>=price[i]):print products[i],price[i]print "+---------------------------------+"print u"輸入exit可以退出購買"choice = raw_input("please choice a shop to buy:")F_choice = choice.strip()#去除空格,格式化輸出。#退出迴圈if F_choice == "exit":breakif F_choice in products:product_price = price[products.index(F_choice)] #取出產品價格print "+---------------------------------+"print u"你要購買的商品以及價格:",F_choice,product_priceprint u"商品正在加入購物列表,請稍等"if salary > product_price:shop_list.append(F_choice)salary = salary - product_priceprint "+---------------------------------+"print u"你已經成功購買了%s" % F_choiceprint u"你的餘額還有:", salaryprint u"你已經購買的商品有:", shop_listprint "+---------------------------------+"else:passelse:print u"你輸入的商品不在商品列表裡,請重新輸入!"return salary#轉賬或者提現def Transfer_accounts(salary):print u"每次轉賬和提現收取百分之5的服務費."inputs = int(raw_input("please input you should how much money:"))SS = inputs*0.05zong = inputs + SSif ( salary < zong or salary < input):print u"餘額不足"else:salary = salary - zongreturn salary#菜單def menu(salary): print u"""Welcome to use ATM automatic teller machine If the machine failure please contact ATM\t """ while True: print "\t(1) shop" print "\t(2) Transfer accounts" print "\t(3) quit" choices = raw_input("Please choices:").strip() if len(choices) == 0: continue if choices == ‘1‘: shopping(salary) elif choices == ‘2‘: Transfer_accounts(salary) else: print "Please pay attention to the property security" exit()if __name__ == ‘__main__‘:login()if a == 1:menu(salary)else:pass
650) this.width=650;" src="https://s4.51cto.com/wyfs02/M00/9C/61/wKioL1lvgfSCMn1oAAQF1aT1ixQ444.png" title="打賞.png" alt="wKioL1lvgfSCMn1oAAQF1aT1ixQ444.png" />
本文出自 “天道酬勤” 部落格,請務必保留此出處http://taindaochouqin.blog.51cto.com/12995943/1949133
python實現atm機的部分功能