1、Python用戶交互
程序難免會與用戶產生交互。
舉個例子,你會希望獲取用戶的輸入內容,並向用戶打印出一些返回的結果。我們可以分別通過 input() 函數與 print()函數來實現這一需求。
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 name = input("name:") 6 age = int(input("age:")) 7 job = input("job:") 8 salary = int(input("salary:")) 9 10 info = """ 11 --------info of {0}------- 12 name:{0} 13 age:{1} 14 job:{2} 15 salary:{3} 16 """.format(name,age,job,salary) 17 18 print(info) 19 20 21 # name:VisonWong 22 # age:27 23 # job:code farmer 24 # salary:10000 25 # 26 # --------info of VisonWong------- 27 # name:VisonWong 28 # age:27 29 # job:code farmer 30 # salary:10000
注:需要注意的是因為年齡與薪金都是數字,所以強制轉化為整形。
2、Python邏輯控制
if語句:
Python 編程中 if 語句用於控制程序的執行,基本形式為:
1 if 判斷條件: 2 執行語句…… 3 else: 4 執行語句……
其中"判斷條件"成立時(非零),則執行后面的語句,而執行內容可以多行,以縮進來區分表示同一范圍。
else 為可選語句,當需要在條件不成立時執行內容則可以執行相關語句,具體例子如下:
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 6 name = input('請輸入用戶名:') 7 pwd = input('請輸入密碼:') 8 9 if name == "VisonWong" and pwd == "cmd": 10 print("歡迎,Vison!") 11 else: 12 print("用戶名和密碼錯誤!") 13 14 15 # 請輸入用戶名:VisonWong 16 # 請輸入密碼:cmd 17 # 歡迎,Vison!
當判斷條件為多個值時,可以使用以下形式:
1 if 判斷條件1: 2 執行語句1…… 3 elif 判斷條件2: 4 執行語句2…… 5 elif 判斷條件3: 6 執行語句3…… 7 else: 8 執行語句4……
實例如下:
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 age = 27 6 7 user_input = int(input("input your guess num:")) 8 9 if user_input == age: 10 print("Congratulations, you got it !") 11 elif user_input < age: 12 print("Oops,think bigger!") 13 else: 14 print("Oops,think smaller!") 15 16 17 # input your guess num:25 18 # Oops,think bigger!
for語句:
Python for循環可以遍歷任何序列的項目,如一個列表或者一個字符串。
for循環的語法格式如下:
1 for iterating_var in sequence: 2 statements(s)
實例1:
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 for letter in 'Python': # 第一個實例 6 print('當前字母 :', letter) 7 8 fruits = ['banana', 'apple', 'mango'] 9 for fruit in fruits: # 第二個實例 10 print('當前水果 :', fruit) 11 12 print("Good bye!") 13 14 15 # 當前字母 : P 16 # 當前字母 : y 17 # 當前字母 : t 18 # 當前字母 : h 19 # 當前字母 : o 20 # 當前字母 : n 21 # 當前水果 : banana 22 # 當前水果 : apple 23 # 當前水果 : mango 24 # Good bye!
實例2:
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 fruits = ['banana', 'apple', 'mango'] 6 for index in range(len(fruits)): 7 print('當前水果 :', fruits[index]) 8 9 print("Have fun!") 10 11 12 # 當前水果 : banana 13 # 當前水果 : apple 14 # 當前水果 : mango 15 # Have fun!
while語句:
Python 編程中 while 語句用於循環執行程序,即在某條件下,循環執行某段程序,以處理需要重復處理的相同任務。其基本形式為:
1 while 判斷條件: 2 執行語句……
執行語句可以是單個語句或語句塊。判斷條件可以是任何表達式,任何非零、或非空(null)的值均為true。
當判斷條件假false時,循環結束。
實例:
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 count = 0 6 while (count < 9): 7 print('The count is:', count) 8 count += 1 9 10 print("Have fun!") 11 12 13 # The count is: 0 14 # The count is: 1 15 # The count is: 2 16 # The count is: 3 17 # The count is: 4 18 # The count is: 5 19 # The count is: 6 20 # The count is: 7 21 # The count is: 8 22 # Have fun!
while 語句時還有另外兩個重要的命令 continue,break 來跳過循環。
continue 用於跳過該次循環,break 則是用於退出循環。
continue實例:
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 i = 0 6 while i <10: 7 i += 1 8 if i < 5: 9 continue # 不往下走了,直接進入下一次loop 10 print("loop:", i) 11 12 13 # loop: 5 14 # loop: 6 15 # loop: 7 16 # loop: 8 17 # loop: 9 18 # loop: 10
break實例:
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 i = 0 6 while i <10: 7 i += 1 8 if i > 5: 9 break # 不往下走了,直接跳出整個loop 10 print("loop:", i) 11 12 13 # loop: 1 14 # loop: 2 15 # loop: 3 16 # loop: 4 17 # loop: 5