Python--面向過程編程

來源:互聯網
上載者:User

標籤:較差   register   utf-8   --   def   就是   pen   使用   rac   

面向過程編程是Python兩種編程流派的其中一種,另外一種是物件導向編程,這篇部落格只討論面向過程編程:

1、概念

面向過程的核心是過程二字,過程就是解決問題的步驟,他就像是設計工廠的一條流水線,是一種機械式的思維方式

2、優點

複雜的問題流程化,簡單化

 

3、編程執行個體

使用者註冊:

 1 import json 2  3  4 def interactive(): 5     name = input(‘>>: ‘).strip() 6     pwd = input(‘>>: ‘).strip() 7     return { 8         ‘name‘: name, 9         ‘pwd‘: pwd10     }11 12 13 def check(user_info):14     is_valid = True15 16     if len(user_info[‘name‘]) == 0:17         print(‘使用者名稱不可為空‘)18         is_valid = False19 20     if len(user_info[‘pwd‘]) < 6:21         print(‘密碼不能少於6位‘)22         is_valid = False23 24     return {25         ‘is_valid‘: is_valid,26         ‘user_info‘: user_info27     }28 29 30 def register(check_info):31     if check_info[‘is_valid‘]:32         with open(‘db.json‘, ‘w‘, encoding=‘utf-8‘) as f:33             json.dump(check_info[‘user_info‘], f)34 35 36 def main():37     user_info = interactive()38     check_info = check(user_info)39     register(check_info)40 41 42 if __name__ == ‘__main__‘:43     main()

過程分解,編程簡單,但是後期更改複雜,比如增加一個填寫email的選項,代碼如下:

 1 import json 2 import re 3  4  5 def interactive(): 6     name = input(‘>>: ‘).strip() 7     pwd = input(‘>>: ‘).strip() 8     email = input(‘>>: ‘).strip() 9     return {10         ‘name‘: name,11         ‘pwd‘: pwd,12         ‘email‘: email13     }14 15 16 def check(user_info):17     is_valid = True18 19     if len(user_info[‘name‘]) == 0:20         print(‘使用者名稱不可為空‘)21         is_valid = False22 23     if len(user_info[‘pwd‘]) < 6:24         print(‘密碼不能少於6位‘)25         is_valid = False26 27     if not re.search(r‘@.*\.com$‘, user_info[‘email‘]):28         print(‘郵箱格式不合法‘)29         is_valid = False30 31     return {32         ‘is_valid‘: is_valid,33         ‘user_info‘: user_info34     }35 36 37 def register(check_info):38     if check_info[‘is_valid‘]:39         with open(‘db.json‘, ‘w‘, encoding=‘utf-8‘) as f:40             json.dump(check_info[‘user_info‘], f)41 42 43 def main():44     user_info = interactive()45     check_info = check(user_info)46     register(check_info)47 48 49 if __name__ == ‘__main__‘:50     main()

從以上代碼可以看出,面向過程編程擴充性較差,有一種牽一髮而動全身的影響,

使用情境:適用於對擴充性要求不是很高的項目

Python--面向過程編程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.