python第一周小测验_Python第一周小结
一、慣例
Code:
Output:
二、字符串變量
Code:
Output:
三、輸入變量
Code:
Output:
說(shuō)明:Python可以自動(dòng)檢測(cè)輸入的類(lèi)型。如果整型的數(shù)值,輸入了一個(gè)float或者其他的算七八糟的東西,不能轉(zhuǎn)換成整數(shù),執(zhí)行報(bào)錯(cuò);float型數(shù)值的處理同樣如此。
四、密文輸入
Code:
Output:
說(shuō)明:
1、python使用 import 導(dǎo)入一個(gè)庫(kù),相當(dāng)于include
2、getpass.getpass方法,實(shí)現(xiàn)不回顯方式的輸入,用于諸如輸入密碼等場(chǎng)合
3、getpass.getpass方法只能在命令行中執(zhí)行,無(wú)法在IDE中執(zhí)行,所以結(jié)果是在cmd中運(yùn)行顯示的
4、windows cd 同Linux cd,windows dir 同Linux ls
五、if……else……
Code:
Output:
六、while
code:
Output:
說(shuō)明:對(duì),你沒(méi)看錯(cuò),這貨有else。。。。while……else
七、for
Code:
Output:
說(shuō)明:
1、counter相當(dāng)于計(jì)數(shù) i,range(0,10,1)從0開(kāi)始到10結(jié)束,步長(zhǎng)為1增加
2、不同于C語(yǔ)言的是,退出循環(huán)時(shí),counter等于9 而不是10
3、步長(zhǎng)必須是整數(shù)
步長(zhǎng)是4時(shí)結(jié)果是:
練習(xí)+預(yù)習(xí):
任務(wù)一:編寫(xiě)登陸接口
知識(shí)點(diǎn):文件操作
要求:
一、輸入用戶(hù)名和密碼
二、認(rèn)證成功后顯示歡迎信息
三、糾錯(cuò)三次后鎖定
#Author XYM
usrname_input = input("usrname:")
counter = 0 #輸入密碼次數(shù)
flag = 0 #是否有這個(gè)用戶(hù)
file = open("logmessage.txt")
linenum = -1
try:
#將文件讀到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()
#寫(xiě)回
file = open('logmessage.txt','w')
try:
file.writelines(flist)
finally:
file.close()
不熟悉,寫(xiě)的真惡心
任務(wù)二:多級(jí)菜單
知識(shí)點(diǎn):列表和字典
要求:
一、二級(jí)菜單
二、可一次選擇進(jìn)入各子菜單
三、不是UI設(shè)計(jì),是交互設(shè)計(jì)
#Author XYM
dict = {
'植物':{
'水果':['蘋(píng)果','香蕉','橘子'],
'蔬菜':['茄子','土豆','辣椒']
},
'動(dòng)物':{
'陸地':['雞','鴨','鵝'],
'天空':['鷹','雀','燕'],
'海洋':['魚(yú)','蝦','蟹'],
}
}
Biology_kind = list(dict.keys())
while True:
for kind in Biology_kind:
print(Biology_kind.index(kind)+1,kind)
select_kind = input("請(qǐng)選擇一種生物物種(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ǐng)選擇一種分類(lèi)(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("輸入非法,請(qǐng)重新輸入")
else:
print("選擇正確編號(hào)")
elif select_kind == 'q':
break
else:
print("輸入非法,請(qǐng)重新輸入")
參考鏈接:http://www.cnblogs.com/pyramid1001/p/5803294.html
總結(jié)
以上是生活随笔為你收集整理的python第一周小测验_Python第一周小结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: centos7 nat模式配置静态ip_
- 下一篇: python实现翻译功能_Python爬