005 Python 基礎特性

上一課做業講解app

1 f = open("111. 111", "w")
2 f.writelines("abcdefg.........")
3 f.close

  新建一個111.111 以寫的方式spa

  向文件寫入一行字符串code

  關閉文件,纔會寫入,不關閉不會把緩衝區內容寫入blog

 

 1 # -*- coding: UTF-8 -*-
 2 count = 3    
 3 filename = "user.txt"    #Python 沒有常量
 4 userinfos = []        #保存到列表裏面
 5 userfile = open(filename)
 6 for userinfo in userfile.readlines():
 7     userinfos.append(userinfo)
 8 userfile.close()
 9 
10 while count > 0:#嘗試3次以後
11     for userinfo in userinfos:
12         print(userinfo)
13         count -= 1
14 else:
15     print("\r\n嘗試次數已達上限!")

 

 判斷登錄代碼字符串

 1 # -*- coding: UTF-8 -*-
 2 count = 3    
 3 filename = "user.txt"    #Python 沒有常量
 4 userinfos = []        #保存到列表裏面
 5 userfile = open(filename)
 6 for userinfo in userfile.readlines():
 7     userinfos.append(userinfo)
 8 userfile.close()
 9 
10 while count > 0:#嘗試3次以後
11     user = input("請輸入帳號:")
12     psw = input("請輸入密碼:")
13     for userinfo in userinfos:
14         usercmp = user + " " + psw + "\n"
15         print(usercmp)
16         print(userinfo)
17         if userinfo == usercmp:
18             print("成功")
19             break
20     count -= 1
21 else:
22     print("\r\n嘗試次數已達上限!")

 

 字符串的分割input

   split()it

1 # -*- coding: UTF-8 -*-
2 s = "111 222 333 444 555 666 777"
3 print(s.split(" ")) #空格分隔
4 #會返回一個列表

  若是給split()不傳遞參數 也會使用空格分割的class

 

 實現方式1:coding

 1 # -*- coding: UTF-8 -*-
 2 count = 3    
 3 filename = "user.txt"    #Python 沒有常量
 4 userinfos = []        #保存到列表裏面
 5 userfile = open(filename)
 6 for userinfo in userfile.readlines():
 7     userinfos.append(userinfo)
 8 userfile.close()
 9 
10 while count > 0:#嘗試3次以後
11     user = input("請輸入帳號:")
12     psw = input("請輸入密碼:")
13     for userinfo in userinfos:
14         usercmp = userinfo.split()
15         if usercmp[0] == user and usercmp[1] == psw:
16             print("登錄成功")
17             break;
18     count -= 1
19 else:
20     print("\r\n嘗試次數已達上限!")

 

● 實現方式2:file

 1 # -*- coding: UTF-8 -*-
 2 count = 3    
 3 filename = "user.txt"    #Python 沒有常量
 4 userinfos = []        #保存到列表裏面
 5 userfile = open(filename)
 6 for userinfo in userfile.readlines():
 7     userinfos.append(userinfo)
 8 userfile.close()
 9 
10 while count > 0:#嘗試3次以後
11     user = input("請輸入帳號:")
12     psw = input("請輸入密碼:")
13     for userinfo in userinfos:
14         usercmp, userpsw, *other = userinfo.split()    #*other 可變長的數據
15         print(usercmp)
16         print(userpsw)
17     count -= 1
18 else:
19     print("\r\n嘗試次數已達上限!")

 

● 切片生成

  s[i]  從i項目s 來源0

  s[i:j]  從i 到的 s

  s[i:j:k]  從 i 到 j  k是長度

1 # -*- coding: UTF-8 -*-
2 mylist = list(range(100))
3 print(mylist)

   生成一個大的

1 # -*- coding: UTF-8 -*-
2 mylist = list(range(100))
3 print(mylist)
4 print(mylist[::2])    #每兩個取值一次
5 print(mylist[-1:0:2])    #最後一個 到0
6 print(mylist[-10:0])  #倒數第十個每次增長2查查
7 print(mylist[1:10])  #倒數第十個每次增長2查查

 

● 列表遍歷

mylist = list(range(100))
length = len(mylist)
for i in range(length):
    print(i)

 

● 字典的遍歷

1 # -*- coding: UTF-8 -*-
2 mydict = {1:"a", 2:"b", 3:"c", 4:"d"}
3 for x in mydict:
4     print(x)                #會打印出來key
5     print(mydict[x])        #把key傳遞進去會顯示
1 # -*- coding: UTF-8 -*-
2 mydict = {1:"a", 2:"b", 3:"c", 4:"d"}
3 for x in mydict.keys():        #打印key
4     print(x)        
5 for x in mydict.values():        #打印values
6     print(x)                
7 for x in mydict.items():
8     print(x)                

相關文章
相關標籤/搜索