python第一周小测验_Python第一周小结
一、慣例
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 = -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()
#寫回
file = open('logmessage.txt','w')
try:
file.writelines(flist)
finally:
file.close()
不熟悉,寫的真惡心
任務二:多級菜單
知識點:列表和字典
要求:
一、二級菜單
二、可一次選擇進入各子菜單
三、不是UI設計,是交互設計
#Author XYM
dict = {
'植物':{
'水果':['蘋果','香蕉','橘子'],
'蔬菜':['茄子','土豆','辣椒']
},
'動物':{
'陸地':['雞','鴨','鵝'],
'天空':['鷹','雀','燕'],
'海洋':['魚','蝦','蟹'],
}
}
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
總結
以上是生活随笔為你收集整理的python第一周小测验_Python第一周小结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos7 nat模式配置静态ip_
- 下一篇: python向mysql中添加数据_通过