國三資訊科技

Top Programming Lanuages

Python教學 | W3School | 初學者好用python工具 https://thonny.org/

turtle 龜圖學(繪圖工具) https://docs.python.org/zh-tw/3/library/turtle.html

請使用 https://replit.com/languages/python3 練習Python程式

寫下第一個Python程式

print('Hello, world!')

再練習進階段程式

a = input('請輸入你的姓名')

print('Hello' , a)

練習計算功能

a=10

b=2

print(a+b)

print(a-b)

print(a*b)

print(a/b) #ab的商數

print(a%b) #ab的餘數

print(a**b) #a的b次方

練習Python程式玩CodeCombat:逃出地城! 遊戲

https://hourofcode.com/cocom

一、Python程式練習題

請使用 https://replit.com/languages/python3 練習Python程式

也可以使用Python Tutor練習及研究程式數值變化

(1)平均數計算

a = int(input('請輸入a='))b = int(input('請輸入b='))c = (a+b)/2print('平均數為 ' , c)

(2)學期成績計算

a = int(input('輸入作業成績 = '))b = int(input('輸入測驗成績 = '))c = int(input('輸入平時表現成績 = '))grades = a*0.4 + b*0.4 + c*0.2print('學期成績 = ' , grades)if grades < 60 : print('不及格')else: print('及格')

for i in range(啟始值,結束值,間格值):

for i in range(1,5,1):

print('*'*i)

挑戰題

j=2

for i in range(1,6,2):

print(' '*j, '*'*i,end='')

j = j-1

print()

(3)累加計算

N = int(input('請輸入累加至N='))sum = 0for a in range(1,N+1,1): sum = sum + aprint('累加的和為',sum)

(4)3的倍數(1~100以内3的倍數)

for i in range(1,100): if i%3==0: print(i)

(5)累積計算

N = int(input('請輸入數字N '))product = 1for a in range(1,N+1,1): product = product * aprint('1*2*...*N = ',product)

(6)密碼輸入3次驗證

password = 137 #密碼n = 1 #次數a = int(input('請輸入密碼'))while a != password and n!=3: print('密碼錯誤第',n,'次') n=n+1 a = int(input('請輸入密碼'))if a==password: print('歡迎使用本系統')else: print('密碼輸入錯誤3次,帳號已被鎖定')

(7)九九乘法法

for i in range(1,10): for j in range(1,10): print('%3d' %(i*j),end=' ') print()

(8)求因數

n = int(input('please input a number , to show factors : '))factors = []for i in range(1,n+1): if n%i == 0: factors.append(i)print(factors)

(9)溫度轉換

val = input("請輸入溫度,例如:32C ")if val[-1] in ["C","c"]: f = 1.8 * float(val[0:-1]) + 32 print("轉換後的溫度為: %.2f F"%f)elif val[-1] in ["F","f"]: c = (float(val[0:-1]) - 32)/1.8 print("轉換後的溫度為: %.2f C"%c)else: print("輸入溫度格式有誤")

(10)抽獎程式

from random import randintn = 30box = []for i in range(1,n+1): position = randint(0, i-1) box.insert(position, i)print(box)print('第一特獎:' + str(box[0]))print('第二特獎:' + str(box[1]))print('第三特獎:' + str(box[2]))

(11)往前位移字母7位(密碼學)

#往前位移字母7位函數def Calc(sInput): iCnt = 0 sOutput="" while(iCnt<len(sInput)): sOutput += chr(ord(sInput[iCnt:iCnt+1:1])-7) iCnt +=1 sOutput += "\n" return(sOutput)inputword = input('please input a letter to displace: ')print(Calc(inputword))

(12)求最大公因數/最小公倍數

#最大公因數函數def gcd(n,m): if(m==0): return n else: return gcd(m,n%m)#最小公倍數函數def lcm(n,m): return int(n*m/gcd(n,m))a = int(input('please input a number a = '))b = int(input('please input a number b = '))if (a>b): print('最大公因數=',gcd(a,b)) print('最小公倍數=',lcm(a,b))else: print('最大公因數=',gcd(b,a)) print('最小公倍數=',lcm(b,a))

(13)直角三角形求第3邊長

import matha = int(input('請輸入直角三角形的一邊長a= '))b = int(input('請輸入直角三角形的一邊長b= '))c = math.sqrt(a*a + b*b)d = math.sqrt(abs(a*a - b*b))triangle1 = [a,b,c]triangle1.sort()triangle2 = [a,b,d]triangle2.sort()print('直角三角形的三邊為 ','%.2f' %triangle1[0],'%.2f' %triangle1[1],'%.2f' %triangle1[2])print('直角三角形的三邊為 ','%.2f' %triangle2[0],'%.2f' %triangle2[1],'%.2f' %triangle2[2])

(14)計算數字和

num = int(input('請輸入數字'))numstr = str(num)numlen = len(str(num))numlist = []for i in range(0,numlen):numlist.append(numstr[i:i+1])sum = 0for j in range(0,len(numlist)): sum += int(numlist[j])print(sum)

(15)判斷是否為迴文

txt = input('請輸入文字')if str(txt)==''.join(reversed(txt)): print('迴文')else: print('不是迴文')

(挑戰題1)蒙地卡羅方法計算圓周率pi

參考資料來源 | 蒙地卡羅方法from random import randomfrom time import perf_counterfrom math import sqrtPoints=int(input('蒙地卡羅方法計算圓周率,請輸入計算點數,點數越多越精準'))cnt = 0start = perf_counter()for i in range(Points): x,y = random(),random() dis = sqrt(x**2+y**2) if dis <= 1.0: cnt += 1pi = 4*cnt/Pointsprint("蒙地卡羅方法計算出來的圓周率pi={:.6f}".format(pi))print("計算花的時間為:{:.6f}秒".format(perf_counter()-start))

二、用Python繪圖

turtle 龜圖學(繪圖工具) https://docs.python.org/zh-tw/3/library/turtle.html

請使用Thonny Python IDE工具練習用turtle繪圖

也可用google 帳號登入使用 https://replit.com/languages/python3 繪圖

1.畫旋轉正方形

from turtle import * #匯入turtlecolor('blue') #藍色 pendown() #下畫筆 for i in range(0,12): #重覆12次 for j in range(0,4): #重覆4次 forward(100) #前進100 right(90) #右轉90度 right(30) #右轉30度penup() #起畫筆

2.畫擴散正方形

from turtle import * #匯入turtlecolor('blue') #藍色pendown() #下畫筆s = 0 #設變數s為0for i in range(0,20): #重覆20次 s = s +20 #s加20 forward(s) #前進s距離 right(90) #轉90度penup() #起畫筆

三、用Python練習排序演算法

請使用 Python Tutor練習及研究程式數值變化

(1)氣泡排序法

def bubble_sorted(iterable): new_list = list(iterable) list_len = len(new_list) for i in range(list_len): for j in range(list_len - i - 1): if new_list[j] > new_list[j + 1]: new_list[j], new_list[j + 1] = new_list[j + 1], new_list[j] return new_listtestlist = [27, 33, 28, 4, 2, 26, 13, 35, 8, 14]print('sorted:', bubble_sorted(testlist))

(2)插入排序法

def insert_sorted(lists): count = len(lists) for i in range(1, count): key = lists[i] j = i - 1 while j >= 0: if lists[j] > key: lists[j+1] = lists[j] lists[j] = key j -= 1 return lists
testlist = [27, 33, 28, 4, 2, 26, 13, 35, 8, 14]print('sorted:', insert_sorted(testlist))

(3)快速排序法

def quick_sorted(data, left, right): # 輸入資料,和從兩邊開始的位置 if left >= right : # 如果左邊大於右邊,就跳出function return i = left # 左邊的代理人 j = right # 右邊的代理人 key = data[left] # 基準點 while i != j: while data[j] > key and i < j: # 從右邊開始找,找比基準點小的值 j -= 1 while data[i] <= key and i < j: # 從左邊開始找,找比基準點大的值 i += 1 if i < j: # 當左右代理人沒有相遇時,互換值 data[i], data[j] = data[j], data[i] # 將基準點歸換至代理人相遇點 data[left] = data[i] data[i] = key quick_sorted(data, left, i-1) # 繼續處理較小部分的子循環 quick_sorted(data, i+1, right) # 繼續處理較大部分的子循環
data = [27, 33, 28, 4, 2, 26, 13, 35, 8, 14]quick_sorted(data,0,len(data)-1)print(data)

四、Python專題

(1)1A2B猜數字遊戲

n系統先隨機產生4個0∼9不重複的數字,再讓使用者輸入4個數字,再將輸入的數字與答案進行比對後,用「幾A幾B」的方式提示使用者是否正確。其中:A 代表數字正確,且位置正確;B 代表數字正確,但位置錯誤。n使用者有輸入的8次機會中, 如果沒猜到正確數字,則遊 戲結束,並顯示「作答已達 8次,遊戲結束,目標數字是⋯」;n如果在8次機會中,使用者輸入的數字完全正確, 則出現「您答對了,數字是⋯」。

from random import randintanswer = ''count_a = 0count_b = 0times = 1numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]random_answer = []for i in range(4): j = randint(0,9-i) random_answer.append(numbers[j]) numbers.remove(numbers[j]) i = i +1for i in range(4): answer = answer + str(random_answer[i])print(answer)input_number = input('請輸入您猜測的 4 個數:')while times < 8 and input_number != answer: for i in range(4): for j in range(4): if i==j and input_number[i]==answer[j]: count_a = count_a + 1 elif input_number[i]==answer[j]: count_b = count_b + 1 print(str(count_a)+'A'+str(count_b)+'B') count_a=0 count_b=0 times = times + 1 input_number = input('請輸入您猜測的4個數:')if input_number == answer : print('您答對了,正確答案是' + answer)else: print('作答已達8次,遊戲結束,正確答案是 ' + answer)