Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Python 程式設計入門 (三)
Search
EAGOAR 國際教育
November 18, 2016
Education
190
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Python 程式設計入門 (三)
本堂將教您巢狀 if/elif/else 及 while/for 迴圈用法。
EAGOAR 國際教育
November 18, 2016
More Decks by EAGOAR 國際教育
See All by EAGOAR 國際教育
EAGOAR 國際教育簡介
eagoar
0
140
Python 程式設計入門 (二)
eagoar
0
150
Python 程式設計入門 (一)
eagoar
0
340
Other Decks in Education
See All in Education
AIがコードを書く時代に、何を教えるのか / What Should We Teach in the Age of AI-Generated Code?
ichiroc
1
610
HTML5 and the Open Web Platform - Lecture 3 - Web Technologies (1019888BNR)
signer
PRO
3
3.6k
INTRODUCTION TO THE CELL
pawan_pharm
0
330
Introduction to Accounting
bvr7352
0
270
Introduction - Lecture 1 - Human-Computer Interaction (1023841ANR)
signer
PRO
0
3.5k
TISSUE
pawan_pharm
0
280
[2026後期火5] 論理学(京都大学文学部 後期 第1回)「名前はあるのに、誰なのかわからない」
yatabe
0
150
形骸化しない社内勉強会の取り組み - オーナーシップの作り方 / In-house study session
soudai
PRO
1
430
El Clima de España. Tema 2 (PPT)
juanmartin2026
0
19k
25-26 CBT Library top items
cbtlibrary
0
360
Modulo : abc
robintux
0
240
DRONEGEOGRAFÍA: INNOVACIÓN DIDÁCTICA Y FOTOGRAMETRÍA 3D PARA LA COHESIÓN TERRITORIAL Y LA CIUDADANÍA ACTIVA
juanmartin2026
0
40k
Featured
See All Featured
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
710
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
So, you think you're a good person
axbom
PRO
2
2.2k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.7k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
520
How Software Deployment tools have changed in the past 20 years
geshan
2
35k
Darren the Foodie - Storyboard
khoart
PRO
4
4k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
50
10k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
A better future with KSS
kneath
240
18k
Transcript
程式設計入門 (三)
程式控制流程 If/elif/else
電腦看似聰明,但其實是很笨的。它的每個動作,都必須透 過程式來控制它,一個口令一個動作。因此程式設計很重要 的一個工作就是:必須先考慮好各種狀況下的應對方式。 就好像開車一樣,在每個路口都必須決定往那個方向去。這 就是「程式控制流程」,在程式設計裡,是非常重要的概念。
上堂課我們學了 if/elif/else,這堂課我們將學習更複雜 的控制流程 – 巢狀 if/elif/else: EX 3-01.py (範例畫面為
Mac電腦 Python IDLE 畫面)
什麼是巢狀 if/elif/else? 簡單說,就是有多層的 if/ elif/else: score=int(input("請輸入你的分數:")) if score >=
60: print("你通過考試,你的成績是:") if score >= 90: print("Grade A !") elif score >= 80: print("Grade B !") elif score >= 70: print("Grade C !") else: print("Grade D !") else: print("你這次考試沒通過!") EX 3-01.py 第 二 層 第 一 層
在 EX 3-01.py 這個程式裡,第一行除了使用 input( ) 來取得 鍵盤輸入的資料外,還使用了 int( )
這個函式,它是什麼意思 呢?
在程式設計中,有一個重要的概念就是「資料型態」,每一 個變數代表的值都有其特定的資料型態,例如: 在這個簡單範例中,我們使用 type( ) 函式來看 a, b, c 三個變
數代表的資料型態。其中 a 是整數,資料型態是 int ; b 是帶 小數的數字,資料型態是 float (程式設計專有名詞:浮點數 字) ; c 則是字串(一段文字),資料型態 str。
為什麼要搞這麼複雜呢?因為不同的資料型態,計算出來的 結果會不一樣,所以必須先定義清楚,才不會產生錯誤。 例如: 在這範例中,使用 int( ) 函式把 a/b 的結果指定為整數型態, 所得到的計算結果就會不一樣。而
EX 3-01.py 程式中: score=int(input("請輸入你的分數:")) 就是把鍵盤輸入的數值指定為整數資料型態。
if 指令的應用,後面不一定要有 elif 和 else,可以只有 if 或 是 if/elif,或是 if/elif/else,而
elif 更是可以有無限多個。 甚至可以是單行: 大家可以多多練習 if/elif/else 不同的用法!
程式控制流程 迴圈
迴圈也是程式設計一個非常重要的概念,它的目的是讓電腦 重複做一些事,Python 的迴圈指令主要有兩個:while 和 for。我們先看看 while 怎麼使用? while 指令必須先設定一個條件,當條件符合,程式就會重複 執行後面的動作(文字內縮部分的指令),每做完一次就重
新檢查條件是否依舊符合,一直重複到條件不符合為止。
在這個範例中,a 原本等於 0,進入迴圈後每次判斷 a 是否小 於 10,當 a 小於 10
就顯示 a 並加一,一直到 a 不小於 10 才結束迴圈,顯示 “End !”
而 for 通常是在你已經知道(或可預期)重複次數的狀態下使 用,因此必須先設定重複次數,例如:
在這個範例中,我們使用了一個新函式 range( )。 range(1,10) 的意思是,從 1 開始到 10,每次加 1 。
因此在這個 for 迴圈裡,會進行總共 10 次! 程式控制流程是程式設計非常重要的概念,請根據這些範例 程式,自行練習不同的應用和變化喔!下一堂將教您更複雜 的控制流程!
1. 練習巢狀 if/elif/else。 2. 練習 while, for 迴圈。 本堂作業