Author name: admin

Python Ch 3-1 Homework

CH 3 條件敘述 if 作業練習 Ex 3-1-1 請寫一個程式,輸入 x ,並判斷 x 是否滿足  100 < x < 200 後, 輸出結果(如:此數介於100, 200之間)。 Ex 3-1-2 請為以下判斷是否為偶數的程式除錯 a = eval(input(‘Enter a numbers: ‘))if a / 2 = 0: print(a, ‘是偶數’) Ex 3-1-3 請寫一個程式,滿足以下條件。  1. 輸入兩個數字。        2. 找出兩數的最大值。       3.輸出最大值。 Ex 3-1-4 請寫一個程式,滿足以下條件。  1. 輸入兩個數字。   

Python Ch 3-1 Homework Read More »

Python Chapter 3-1

Chapter 3 條件敘述 if Python的條件敘述共有三類:  if,  if … else,  if … elif ….elif … else 1. if 語法 ans = input(‘Do you want some cookies?’)if ans == “y”: print (“Yes, I want some cookies.”) print(“and you?”)print(“over”) ans = input(‘Do you want some cookies?’).upper()# ans = ans.upper()if ans == “Y”: print (“Yes, I want some cookies.”)

Python Chapter 3-1 Read More »

Python Ch2-2 Homework

CH 2-2 變數與資料類型 作業練習 Ex 2-2-1 試修正下列程式的錯誤 num_f = Input(‘輸入一浮點數:’)print(num_f + 200) Ex 2-2-2 請寫一段程式,顯示以下的文字, 待輸入’y’, 顯示最後一行 set3:20 21 22 2324 25 26 2728 29 30 3132 33 34 35Enter y for Yes, n for No: yans = y Ex 2-2-3 攝氏轉華氏溫度的公式是華氏 = 攝氏 *(9/5)+32,請寫一程式,可以輸入攝氏溫度,並輸出華氏溫度,輸出時,欄位寬度設為 5,小數取一位。程式執行的效果如下。 請輸入攝氏溫度:32攝氏溫度 32 度等於華氏溫度 89.6 度 Ex 2-2-4 請分四次輸入一個 0

Python Ch2-2 Homework Read More »

Python Chapter 2-2

Chapter 2 變數與資料類型 2-2 資料類型與輸出入 資料的類型 1. 資料的型態(type),有整數(integer)、浮點數(float)和字串(string)。相對應的轉換函式為int()、float()、str()。     還有一種布林值(Boolean),就是真假值(判斷正確與否),True或False lunch = 100dinner = 180totalcost = lunch + dinnerprint(totalcost)print(type(totalcost)) # int 整數yearcost = 35000daycost= yearcost / 365print(daycost)print(type(daycost)) # float 浮點數mysentence = “哈囉, 大家好”print(mysentence)print(type(mysentence)) # str 字串 a=12;b=34print(a+b) #46print(str(a)+str(b)) #1234print(str(a)+”xxx”+str(b)) #12xxx34c=’12’;d=’34’print(c+’yyy’+d) #12yyy34print(int(c)+int(d)+100) #146 a=input(“輸入第一個值:”)b=input(“輸入第二個值:”)print(a+b) print(‘I’m “python”‘)print(2>3)print(9>3 and 9>5 )print(4>5 or 3>1)print(not 2<5) 2.『%』是輸出控制字元    

Python Chapter 2-2 Read More »

Python Ch2-1 Homework

CH 2-1 變數宣告與基本運算 作業練習 Ex 2-1-1 請寫一個程式,可以讀入(input)某個字串,並且輸出特定的字串。例如:輸入字串 “Eden”, 則輸出 “hello, Eden” Ex 2-1-2 請依序輸入長方形的長與寬,賦予變數名稱分別為 length與 width ,並計算出面積。 Ex 2-1-3 假設某次考試成績分別為:  83、75、96、68、88。 (1) 請寫出程式儲存以上資料。 (2) 計算總和。 (3) 輸出總和與平均。 Ex 2-1-4 鉛筆一支 5 元,一打 50 元。小明需要幫班上每位同學買一枝鉛筆,請問要多少錢?由於小明很注重環保,他絕不會為了省錢而多買任何不需要的東西。也就是說,小明買的鉛筆數量一定等於班上的人數。 請寫出一段程式, 輸入”班上人數”, 就立即輸出總金額。(hint: 充分利用商數與餘數的算法) Ex 2-1-5 請輸入一個四位數,並將其反向輸出此四個數字。例如,輸入 2695,那可以輸出 5 9 6 2。(數字之間留空格) Ex 2-1-6 若有一個五位數,請問如何分解為兩個一組。例如, a=35869,那如何分別得到 35,58,86,69 等 4 個數字。 Ex

Python Ch2-1 Homework Read More »

Python Chapter 2-1

Chapter 2 變數與資料類型 2-1 變數宣告與基本運算 變數的命名原則 1. 變數僅能以大小寫的英文字母或底線『_』開頭。變數由字母、底線開頭後,僅可由字母、底線及數字組合而成,但不得包含空白。例如,以下是合法的變數名稱。 Aa i sum _sum a123 AB1574 _a_b 2. 以下是不合法的變數名稱 7eleven     #不能以數字開頭 %as           #不能以符號開頭 A=             #不能有 = 號 Sum!        #不能有 ! 號 Age#3      #不能含 # 號 a c            #不能含空白

Python Chapter 2-1 Read More »