今天是看這本書Automate the Boring Stuff with Python(Python編程快速上手--讓繁瑣工作自動化)
第2章節(Flow Control)所做的練習
用Notepad++打下列的程式碼,
另存為Chapter_2.py. 我附上中文注釋方便好讀.
# -*- coding: UTF-8 -*- # http://juilin77.blogspot.com/ # v20181203 # Automate The Boring Stuff With Python - Chapter 2 # 導入一個random模組 import random # 練習布爾運算: 注意順序:算術 > 大小 > not > and > or print(2 + 2 == 2 * 2 and not True or False and 2 + 3 < 3 + 2 or True) print ('#' * 10) # 練習while用法, 及輸入input Name = '' while Name != 'AI': print ('Who are you?') Name = input('Typing "AI": ') print ('#' * 10) # 練習 for in range, print寫法注意 for i in range (0, 5, 2): print('The Number (' + str(i) + ')') print ('#' * 10) # 練習while用法 i = 1 while i <= 10: print (i) i = i + 1 print ('#' * 10) # 產生0-2亂數, 當亂數為1 , 2 或其他數時, 印出不同的字 spam = random.randint(0, 3) print('spam =', spam) if spam == 1: print('Hello') elif spam == 2: print('Howdy') else: print('Greetings!')
然後用Windows的cmd, 執行python.
D:\Tech\Python\Automate The Boring Stuff With Python>python Chapter_2.py True ########## Who are you? Typing "AI": A Who are you? Typing "AI": AI ########## The Number (0) The Number (2) The Number (4) ########## 1 2 3 4 5 6 7 8 9 10 ########## spam = 1 Hello D:\Tech\Python\Automate The Boring Stuff With Python>
完成
Reference:
Automate the Boring Stuff with Python
Python编程快速上手:让繁琐工作自动化
ISBN-10: B01N6B9BSA
https://www.amazon.com/Python%E7%BC%96%E7%A8%8B%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B-%E8%AE%A9%E7%B9%81%E7%90%90%E5%B7%A5%E4%BD%9C%E8%87%AA%E5%8A%A8%E5%8C%96-%E7%BE%8E-Al-Sweigart%EF%BC%88%E6%96%AF%E7%BB%B4%E5%8A%A0%E7%89%B9%EF%BC%89/dp/B01I0XN8XY/ref=sr_1_1?ie=UTF8&qid=1543814481&sr=8-1&keywords=9787115422699
官網:
https://automatetheboringstuff.com/
最初發表 / 最後更新: 2018.12.03 / 2018.12.03
0 comments:
張貼留言