python第四周练习题
def shoping(): balance=10000 while True: with open("E:/product_list.txt",'r+') as f: product_list=f.readlines() print(product_list) f.close() allproduct=[] allprice=[] for i in range (0,len(product_list)): productprice=product_list[i].split() productproduct=productprice[0] productprice=productprice[1] allproduct.append(productproduct) allprice.append(productprice) buysomething=input('what do you want to buy? please input number:') #检测账户余额是否足够 if buysomething.isdigit(): buysomething=int(buysomething) print(allprice[buysomething]) if int(allprice[buysomething])<balance: balance-=int(allprice[buysomething]) with open("E:/shop_list.txt",'a') as f2:#放入购物车 f2.write(str(allproduct[buysomething] +' '+ allprice[buysomething]+ '\n')) print('your balance is %s'%balance) with open("E:/shop_list.txt",'r+') as f3:#放入购物车 shopbuy=f3.readlines() print(shopbuy) f3.close() def login(): f=open("E:/2.txt",'r+') #读取user配置文件。 cont=f.readlines() #readlines把读取的行当作元素返回一个列表 f.close() allname=[] #初始化一个用户列表 allpasswd=[] for i in range(0,len(cont)-1): #len获取cont列表的元素数量 onecont=cont[i].split() #循环取一行内容并分割成列表,split()以空格为分隔符分割字符串并返回一个列表。 onename=onecont[0] #循环取一行中的帐号 onepasswd=onecont[1] # allname.append(onename) #循环把每一行取到的帐号追加到用户列表中 allpasswd.append(onepasswd) lf=open("E:/3.txt",'r') lcont=lf.readlines() lf.close() # print(lcont) #打印用户锁文件内容 # print(allname) # print(allpasswd) cont=0 while cont < 3: username=input("login user:").strip() passwd=input("password:") if username not in allname: print("No this accont!") elif (username +"\n") in lcont: print("your account has been locked!Please contact admin!") break else: rel_passwd_index=allname.index(username) #取该帐号在用户列表中的索引,此时用户列表的索引和密码列表的索引是对应的,因此我们同样>取到了该帐号的密码在密码列表的索引 rel_passwd=allpasswd[rel_passwd_index] #取该帐号的真实密码 if passwd==rel_passwd: print("Login success!") break else: print("password is error!") cont+=1 if cont >= 3: print("Excessive password error,your account has been locked!Please contact admin!") nf=open("E:/3.txt",'w') nf.write(username+"\n") nf.close()
ATM作业的购物车和登陆功能两个函数功能实现。还有ATM,交易中心未实现