EX1:哈囉 name = input('請問你的名字是?') print('哈囉!', name, '你好') print('A1','A2','A3') print('hello world') print("A1","A2",'A3',sep='#',end='@') print('hello world') EX2:三數之和 x = int(input('請輸入數字 X:')) y = int(input('請輸入數字 Y:')) z = int(input('請輸入數字 Z:')) print(x + y + z) print('X+Y+Z=', x+y+z ) EX3:求平均數 x = int(input('請輸入數字 X:')) y = int(input('請輸入數字 Y:')) z = int(input('請輸入數字 Z:')) average = (x + y + z)/3 print(average) print(int(average)) print('平均值=',average) print('平均值='+str(average)) EX4:計算學期成績 score1 = int (input('請輸入作業成績:')) score2 = int (input('請輸入測驗成績:')) score3 = int (input('請輸入平時成績:')) grade = score1*0.4 + score2*0.4 +score3*0.2 print('學期成績是:' + str(grade)) print('學期成績是:' ,grade) print('學期成績是:' ,grade ,sep='') if grade < 60: print('不及格') else : print('及格') print(8==5) print(2+6==3+5) print(8!=5) print(2+6!=3+5) print(8>5) print(2>6) print(8<5) print(2<6) print(8>=5) print(2+3>=5) print(2>=6) print(8<=5) print(2+3<=5) print(2<=5) EX5:累加 n=int(input('請輸入數字 n:')) sum=0 for i in range(1,n+1): sum = sum + i print('1+2+...' + str(n) + "=" + str(sum)) list1 = [1,2,3,4,5] list2 = ['john','Mary','Tom'] list3 = ['Hellen', 18, True] print(list1[0]) print(list2[1]) print(list3[-1]) list1 = range(5) list2 = range(1,5) list3 = range(1,5,2) list4 = range(5,1,-1) print(list1[0]) print(list2[1]) print(list3[1]) print(list4[3]) EX6:密碼檢查程式 password = '137' times = 1 password2 = input('請輸入密碼:') while password != password2 and times < 3: print('密碼錯誤') times= times +1 password2 = input('請輸入密碼:') if password== password2: print('歡迎使用本系統') else: print('密碼輸入錯誤3次,帳號被鎖定') print(not(5<8)) print((5>2)and(6<8)) print((2<3)or(5<9)) EX7:任一數的所有因數 n = int(input('請輸入數字 n :')) factors = [] for i in range(1, n+1): if n % i == 0: factors.append(i) print(factors) list1 = ['a','b','c','d','e'] list2 = list1[1:3] list3 = list1[2:] list4 = list1[:3] print(list1) print(list2) print(list3) print(list4) list1 = ['a','b'] list1.append('c') list2 = ['a','b'] list2.remove('b') list3 = ['a','b'] list3.insert(0,'b') list4 = ['a','b'] list4.insert(1,'d') print(list1) print(list2) print(list3) print(list4) EX8:抽獎程式 from random import randint n = 30 box = [] 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])) 程式專題:1A2B from random import randint n = 9 box = [] #設定10個數字,及先後順序,取最前面四個為答案 for i in range(0,n+1): position = randint(0,i) box.insert(position,str(i)) #檢查點 print(box) #將答案用字串顯示 ANSstr='' for i in range(0,4): ANSstr = ANSstr + box[i] print(ANSstr) #答案輸入 """ ANS =[] ANS.insert(0,input('請輸入答案第一個數字:')) ANS.insert(0,input('請輸入答案第二個數字:')) ANS.insert(0,input('請輸入答案第三個數字:')) ANS.insert(0,input('請輸入答案第四個數字:')) #檢查點 print(ANS) """ countA=0 count=1 while (countA!=4 and count!=4): #輸入次數統計 #輸入字串長度偵測 ANSALL=input('請輸入個數字:') len(ANSALL) while len(ANSALL)!=4: ANSALL=input('請輸入答案第四個數字:') #答案比對 countA = 0 countB = 0 for i in range(0,4): for j in range(0,4): if box[i]==ANSALL[j]: if i==j: countA=countA +1 else: countB=countB +1 print('countA:',countA) print('countB:',countB) if countA==4: print('bingo') else: print('no') count=count+1 print(count)