python学习心得-第一天-作业
python学习第一天作业
作业1
用户输入帐号密码进行登陆
用户信息保存在文件内
用户密码输入错误三次后锁定用户
代码:
#__author__ = 'leslie'
#-*-coding:utf-8-*-
#
#1. 用户输入帐号密码进行登陆
#2. 用户信息保存在文件内
#3. 用户密码输入错误三次后锁定用户
import os
import sys
pass_list = open("leslie.txt","r")#预先填写,格式为 姓名:密码
drop_list = open ("caosubo.txt","r")
name_list = []
clock_list =[]
name_dict = {}
for i in pass_list:#将白名单中的信息读取,存储到列表中
name,passWord = i.strip('\n').split(':')
name_list.append(name)
name_dict[name] = password
pass_list.close()
for i in drop_list:#将黑名单的信息读取,存储到列表中
clock_list.append(i.strip('\n'))
drop_list.close()
count = 0
username = input("Please input your name:")
if username in clock_list:
print ("Sorry ,you are in blacklist")
sys.exit()
else:
if username in name_dict:
for count in range(10):
if count < 3:
passwd = input("Please input your passwd:")
if passwd == name_dict[username]:
print ("Welcome!".center(40,'-'))
break
else:
print ("Sorry,your passwd is worry!")
count +=1
continue
if count > 3:
print ("Your name is blacklist")
f = open('caosubo.txt','a')
f.write(username + '\n')
f.close()
sys.exit()
else:
print ("Sorry ,your name is not in")
sys.exit()
作业2
三级菜单:
运行程序输出第一级菜单
选择一级菜单某项,输出二级菜单,同理输出三级菜单
菜单数据保存在文件中
代码:
/#__author__ = 'leslie'
#-*- coding = utf-8 -*-
import sys
import shop
shop_list = shop.shop_list
exit_flag = False
the_choice = []
while exit_flag is not True:
for i in shop_list:
print (i)
choice1 = input ("Please input your choice:")
if choice1 in shop_list:
for a in shop_list[choice1]:
print (a)
choice2 = input ("Please input your choice2:")
if choice2 in shop_list[choice1]:
for c in shop_list[choice1][choice2]:
print (c)
choice3 = input ("Please input your choice3:")
if choice3 in shop_list[choice1][choice2]:
the_choice.append(choice3)
elif choice3 == 'b':
continue
elif choice3 == 'q':
exit_flag = True
else:
print ("No your choice")
continue
elif choice2 == 'b':
continue
elif choice2 == 'q':
exit_flag = True
else:
print ("No your choice")
continue
elif choice1 == 'b':
continue
elif choice1 == 'q':
exit_flag = True
print (the_choice)
break
sys.exit()
else:
print ("No your choice")
continue
作业3
购物车:
商品信息- 数量、单价、名称
用户信息- 帐号、密码、余额
用户可充值
购物历史信息
允许用户多次购买,每次可购买多件
余额不足时进行提醒
用户退出时 ,输出档次购物信息
用户下次登陆时可查看购物历史
商品列表分级显示
代码:
#__author__ = 'leslie'
#-*- condig=utf-8 -*-
#购物车:
#1. 商品信息- 数量、单价、名称
#2. 用户信息- 帐号、密码、余额
#3. 用户可充值
#4. 购物历史信息
#5. 允许用户多次购买,每次可购买多件
#6. 余额不足时进行提醒
#7. 用户退出时 ,输出档次购物信息
#8. 用户下次登陆时可查看购物历史
#9. 商品列表分级显示
import os
import sys
name_list = {
'caosubo':['123456','9999'],'leslie':['2345','8888']
}
shop_list = ['家用电器','日用百货','服装鞋帽']
dianqi = ['电视','洗衣机','电冰箱']
yiyongbaihuo = ['洗衣粉','卫生纸','牙膏']
cloth = ['男装','女装','童装']
tv = [('创维',3000),('乐视',4000)]
washer = [('海尔',5000),('小天鹅',4500)]
icebox = [('海尔',6000),('格力',5500)]
detergent = [('雕牌',10),('立白',8)]
bumf = [('维达',15),('心相印',15)]
toothpaste = [('中华',12),('黑人',15)]
cloth_man = [('杉杉',200),('七匹狼',500)]
cloth_woman = [('普拉达',5000),('迪奥',10000)]
cloth_child = [('小天才',500),('巴拉巴拉',600)]
a = {'家用电器':dianqi,'日用百货':yiyongbaihuo,'服装鞋帽':cloth}
b = {'电视':tv,'洗衣机':washer,'电冰箱':icebox,'洗衣粉':detergent,'卫生纸':bumf,'牙膏':toothpaste,'男装':cloth_man,'女装':cloth_woman,'童装':cloth_child}
shop_car = {}
exit_flag = False
count = 0
while exit_flag is not True:
if count < 3:
username = input("请输入你的姓名:")
password = input("请输入密码:")
if username in name_list:
passwd = name_list[username][0]
salary = int(name_list[username][1])
if password == passwd:
f = open(username,'a')
print ("Welcome".center(40,'-'))
print ("您的账户余额为:%s"%salary)
money = input ("确定是否充值,输入金额,否请输入n:")
if money.isdigit():
money = int(money)
salary = salary + money
print ("你的余额为%s:"%salary)
break
elif money == 'n':
break
else:
print ("请重新输入!")
else:
print ("您的密码有误!请重新输入:")
count += 1
else:
print ("尚未注册")
count += 1
else:
exit()
while exit_flag is not True:
print ("商品分类".center(40,'-'))
for item in enumerate(shop_list):
print (item)
first_choice = input ("请输入你的选择:")
if first_choice.isdigit() :
first_choice = int(first_choice)
if first_choice < len((shop_list)):
shop1 = a[shop_list[first_choice]]
print ("商品分类".center(40,'-'))
for item in enumerate(shop1):
print (item)
second_choice = input ("请输入你的选择:")
if second_choice.isdigit():
second_choice = int(second_choice)
if second_choice < len(shop1):
shop2 = b[shop1[second_choice]]
print ("商品分类".center(40,'-'))
for item in enumerate(shop2):
print (item)
third_choice = input ("请输入你的选择:")
if third_choice.isdigit():
third_choice = int(third_choice)
if third_choice < len(shop2):
shop_choice = shop2[third_choice][0]
number = input ("购买数量:")
if number.isdigit():
number = int(number)
if number > 0:
shop_price = shop2[third_choice][1]*number
else:
print ("您的输入有误!")
else:
print ("您的输入有误!")
if salary > shop_price:
salary = salary - shop_price
shop_car[shop_choice] = number
print (shop_car)
else:
print ("你的余额不足!")
input_money = input ("需要充值吗?请输入金额或是q退出:")
if input_money.isdigit():
input_money = int (input_money)
salary = salary + input_money
print ("您现在的余额为:%s"%salary)
elif input_money == 'q':
pass
else:
print ("对不起你输入有误!")
else:
print ("您的选择有误,请重新选择!")
elif third_choice == 'q':
print ("欢迎下次光临!".center(40,'-'))
print ("您购买的商品如下:%s"%shop_car)
print ("您的余额为:%s"%salary)
exit_flag = True
else:
print ("您的输入有误!")
elif second_choice == 'q':
print ("欢迎下次光临!".center(40,'-'))
print ("您购买的商品如下:%s"%shop_car)
print ("您的余额为:%s"%salary)
exit_flag = True
else:
print ("您的输入有误!")
elif first_choice == 'q':
print ("欢迎下次光临!".center(40,'-'))
print ("您购买的商品如下:%s"%shop_car)
print ("您的余额为:%s"%salary)
exit_flag = True
else:
print ("您的输入有误!")
#将购物车信息存储在已用户名命名的文件中
f = open(username,'a')
bb = str(shop_car)
f.write(bb + '\n')
f.close()
作业4
HAproxy配置文件操作:
根据用户输入输出对应的backend下的server信息
可添加backend 和sever信息
可修改backend 和sever信息
可删除backend 和sever信息
操作配置文件前进行备份 6 添加server信息时,如果ip已经存在则修改;如果backend不存在则创建;若信息与已有信息重复则不操作
配置文件 参考 Http://www.cnblogs.com/alex3714/articles/5717620.html
代码:
#__author__ = 'leslie'
#{'backend': 'www.leslie.com','record':{ 'server': '100.1.7.8' ,'weight': 10,'maxconn': 1000 }}
import shutil
#选项菜单
def choice():
print ("Welcome".center(40,'#'))
choice_list = ["1.查询","2.添加","3.删除","4.修改","q.退出"]
print (choice_list)
#文件备份
def copy():
shutil.copyfile('haproxy','haproxy.txt')
#查询
def query(backend):
flag = False
query_list = []
with open ('haproxy','r',encoding="utf-8") as file:
for line in file:
if line.strip() == 'backend %s'% backend:
flag = True
continue
if line.strip().startswith('backend'):
flag = False
if flag and line.strip():
query_list.append(line.strip())
return query_list
#内容添加
def add (dict_list):
dict_list = eval(dict_list)
backend_name = dict_list.get('backend')
backend_context = (("backend %s")%backend_name)
record_name = dict_list["record"]
record_context = ("server %s %s weight %s maxconn %s"%(record_name['server'],record_name['server'],record_name['weight'],record_name['maxconn']))
record_list = query(backend_name)
flag = False
if record_list:
write_flag = False
with open ('haproxy','r',encoding="utf-8") as file_old ,open ('haproxynew','w',encoding="utf-8") as file_new:
for line in file_old:
if line.strip() == backend_context:
file_new.write("\n" + line)
flag = True
continue
if flag and line.startswith('backend'):
flag = False
if flag:
for line_new in record_list:
if not write_flag:
file_new.write(line_new)
write_flag = True
else:
file_new.write(line)
else:
with open ('haproxy','r',encoding="utf-8") as file_old ,open ('haproxynew','w',encoding="utf-8") as file_new:
for line in file_old:
file_new.write(line)
file_new.write("\n" + backend_context + "\n")
file_new.write(" "*8 + record_context +"\n")
flag = True
if flag is True:
shutil.move('haproxynew','haproxy')
#内容删除
def delete (dict_list):
dict_list = eval(dict_list)
backend_del = dict_list['backend']
record_del = dict_list['record']
backend_context = ('backend %s'%backend_del)
record_context = ("server %s %s weight %s maxconn %s"%(record_del['server'],record_del['server'],record_del['weight'],record_del['maxconn']))
query_list = query(backend_del)
if not query_list:
return
else:
if record_context not in query_list:
print ("IT is not in it ")
return
else:
query_list.remove(record_context)
with open ('haproxy','r',encoding="utf-8") as file_old ,open ('haproxynew','w',encoding="utf-8") as file_new:
flag = False
write_flag = False
for line in file_old:
if line.strip()== backend_context:
file_new.write(line)
flag = True
continue
if flag and line.startswith('backend'):
flag = False
if flag:
if not write_flag:
print (query_list)
for line in query_list:
file_new.write("%s%s\n"%(""*8,line))
write_flag = True
else:
file_new.write(line)
if flag is True:
shutil.move('haproxynew','haproxy')
#内容修改
def change (dict_list):
dict_list = eval(dict_list)
backend_name = dict_list.get('backend')
backend_context = (("backend %s")%backend_name)
record_name = dict_list["record"]
record_context = ("server %s weight %s maxconn %s"%(record_name['server'],record_name['weight'],record_name['maxconn']))
record_context_new =["server %s weight %s maxconn %s"%(record_name['server'],record_name['weight'],record_name['maxconn'])]
record_list = query(backend_name)
record_list_new = str(record_list)
flag = False
if not record_list:
with open ('haproxy','r',encoding="utf-8") as file_old ,open ('haproxynew','w',encoding="utf-8") as file_new:
for line in file_old:
file_new.write(line)
file_new.write("\n" + backend_context +"\n")
file_new.write(" "*8 + record_context +"\n")
flag = True
if record_list:
with open ('haproxy','r',encoding="utf-8") as file_old ,open ('haproxynew','w',encoding="utf-8") as file_new:
for line in file_old:
file_new.write(line)
if line.strip() == backend_context:
if record_list == record_context_new:
print ("It is in now".center(40,"#"))
continue
else:
for line in file_old:
if line.strip().startswith('server'):
line = (" "*8 + record_context + "\n")
file_new.writelines(line)
file_new.flush()
flag = True
if flag is True:
shutil.move('haproxynew','haproxy')
flag = False
while flag is not True:
choice()
choice_list = input ("Please input your choice:").strip()
if choice_list.strip() == '1':
copy()
backend_name = input ("Please input backend:").strip()
result = query(backend_name)
print (result)
elif choice_list.strip() == '2':
copy()
backend_list_dict = (input("Please input your backend:").strip())
add(backend_list_dict)
elif choice_list.strip() == '3':
copy()
backend_list_dict = (input("Please input your backend:").strip())
delete(backend_list_dict)
elif choice_list.strip() == '4':
copy()
backend_list_dict = (input("Please input your backend:").strip())
change(backend_list_dict)
elif choice_list.strip() == 'q':
print ("GoOD BYE".center(40,'#'))
flag = True
exit('quit')
else:
print ("Sorry what is your input is error")
Edit By MaHua
相关文章