一、惯例
Code:
Output:
二、字符串变量
Code:
Output:
三、输入变量
Code:
Output:
说明:Python可以自动检测输入的类型。如果整型的数值,输入了一个float或者其他的算七八糟的东西,不能转换成整数,执行报错;float型数值的处理同样如此。
四、密文输入
Code:
Output:
说明:
1、python使用 import 导入一个库,相当于include
2、getpass.getpass方法,实现不回显方式的输入,用于诸如输入密码等场合
3、getpass.getpass方法只能在命令行中执行,无法在IDE中执行,所以结果是在cmd中运行显示的
4、windows cd 同Linux cd,windows dir 同Linux ls
五、if……else……
Code:
Output:
六、while
code:
Output:
说明:对,你没看错,这货有else。。。。while……else
七、for
Code:
Output:
说明:
1、counter相当于计数 i,range(0,10,1)从0开始到10结束,步长为1增加
2、不同于C语言的是,退出循环时,counter等于9 而不是10
3、步长必须是整数
步长是4时结果是:
练习+预习:
任务一:编写登陆接口
知识点:文件操作
要求:
一、输入用户名和密码
二、认证成功后显示欢迎信息
三、纠错三次后锁定
#Author XYM usrname_input = input("usrname:")counter = 0 #输入密码次数flag = 0 #是否有这个用户file = open("logmessage.txt")linenum = -1try: #将文件读到list中 flist = file.readlines() #遍历list for logmessage in flist: if logmessage != ‘\n‘: linenum += 1 strlist = logmessage.split(":") usrname = strlist[0] passwd = strlist[1] locked = int(strlist[2]) if usrname == usrname_input: flag = 1 if 0 == locked: print("You are locked,please call the admin") else: while counter < 3: passwd_input = input("passwd:") if passwd == passwd_input: message = usrname + ":" + passwd + ":" + "1" print("Welcome ",usrname_input) break else: counter += 1 print("Err passwd,you have %d chances"%(3-counter)) else: message = usrname+":"+passwd+":"+"0" print("You have try three times,you will be locked") flist[linenum] = message #将list中的字符串末尾添上 \n for str in flist: if str[len(str)-1] != ‘\n‘ : flist[flist.index(str)] = str + ‘\n‘ if flag == 0: print("This usrname is not exit")finally: file.close()#写回file = open(‘logmessage.txt‘,‘w‘)try: file.writelines(flist)finally: file.close()
不熟悉,写的真恶心
任务二:多级菜单
知识点:列表和字典
要求:
一、二级菜单
二、可一次选择进入各子菜单
三、不是UI设计,是交互设计
#Author XYMdict = { ‘植物‘:{ ‘水果‘:[‘苹果‘,‘香蕉‘,‘橘子‘], ‘蔬菜‘:[‘茄子‘,‘土豆‘,‘辣椒‘] }, ‘动物‘:{ ‘陆地‘:[‘鸡‘,‘鸭‘,‘鹅‘], ‘天空‘:[‘鹰‘,‘雀‘,‘燕‘], ‘海洋‘:[‘鱼‘,‘虾‘,‘蟹‘], }}Biology_kind = list(dict.keys())while True: for kind in Biology_kind: print(Biology_kind.index(kind)+1,kind) select_kind = input("请选择一种生物物种(q退出)") if select_kind.isdigit(): if int(select_kind) >0 and int(select_kind) <= len(Biology_kind): name = Biology_kind[int(select_kind)-1] Biology_kind2 = list(dict[name].keys()) while True: for kind in Biology_kind2: print(Biology_kind2.index(kind) + 1, kind) select_kind2=input("请选择一种分类(q退出b返回)") if select_kind2.isdigit(): name2= Biology_kind2[int(select_kind2)-1] Biology = (dict[name][name2]) while True: for kind in Biology: print(kind) operate=input("q退出b返回") if operate==‘b‘: break elif operate ==‘q‘: exit() elif select_kind2 == ‘q‘: exit() elif select_kind2 == ‘b‘: break; else: print("输入非法,请重新输入") else: print("选择正确编号") elif select_kind == ‘q‘: break else: print("输入非法,请重新输入") 参考链接:http://www.cnblogs.com/pyramid1001/p/5803294.html
时间: 2024-10-06 20:56:20