自學python的日記分享
- 2020 年 1 月 16 日
- 筆記
2019.4.22登記
課堂筆記 2019.4.8
在windows環境下,用python寫出第一個程式「hello world」
1 print("Hello World!!!")
View Code
課堂筆記 2019.4.12
在windows環境下,用python寫出第一個用戶交互程式「input」
1 death_age=120 2 3 print("game star") 4 print("") 5 print("") 6 7 name=input("input your name:") 8 age=input("input your age:") 9 10 11 print(name,"still able to live",death_age-int(age),"years")
View Code
課堂筆記2019.4.13
python程式<數字比大小>: 用戶輸入3個數字,輸出最大的數字和最小的數字
1 #My idea 2 3 ''' 4 No1=int(input("please input first number:")) 5 No2=int(input("please input scend number:")) 6 No3=int(input("please input third number:")) 7 8 if No1>No2>No3: 9 print("Max No is No1:",No1,"Min No is No3:",No3) 10 elif No1>No3>No2: 11 print("Max No is No1:",No1,"Min No is No2:",No2) 12 elif No2>No1>No3: 13 print("Max No is No2:",No2,"Min No is No3:",No3) 14 elif No2>No3>No1: 15 print("Max No is No2:",No2,"Min No is No1:",No1) 16 elif No3>No1>No2: 17 print("Max No is No3:",No3,"Min No is No2:",No2) 18 elif No3>No2>No1: 19 print("Max No is No3:",No3,"Min No is No1:",No1) 20 ''' 21 22 23 #teather's idea. only MaxNo,no MinNo 24 25 ''' 26 No1=int(input("please input first number:")) 27 No2=int(input("please input scend number:")) 28 No3=int(input("please input third number:")) 29 30 No=0 31 32 if No1>No2: 33 No=No1 34 if No>No3: 35 print("Max No is:",No) 36 else: 37 print("Max No is:",No3) 38 else: 39 No=No2 40 if No>No3: 41 print("Max No is:",No) 42 else: 43 print("Max No is:",No3) 44 ''' 45 46 #bettet idea 47 48 No1=int(input("please input first number:")) 49 No2=int(input("please input scend number:")) 50 No3=int(input("please input third number:")) 51 52 max_No=0 53 min_No=0 54 55 if No1>No2: 56 max_No=No1 57 if max_No<No3: 58 min_No=No2 59 print("Max No is:",No3,"Min No is:",min_No) 60 else: 61 if No2<No3: 62 min_No=No2 63 print("Max No is:",max_No,"Min No is:",min_No) 64 else: 65 min_No=No3 66 print("Max No is:",max_No,"Min No is:",min_No) 67 else: 68 max_No=No2 69 if max_No<No3: 70 min_No=No1 71 print("Max No is:",No3,"Min No is:",min_No) 72 else: 73 if No1<No3: 74 min_No=No1 75 print("Max No is:",max_No,"Min No is:",min_No) 76 else: 77 min_No=No3 78 print("Max No is:",max_No,"Min No is:",min_No)
View Code
課堂筆記2019.4.14
python的四種運算符:算數運算符,賦值運算符,比較運算符,邏輯運算符。
算數運算符:+,-,*,/,//,%,**
賦值運算符:word="hello"(賦值字元串) , word=23(賦值數字)
比較運算符:<,>,==,!=
邏輯運算符:not , and , or (and和or有短路原則,如果條件1結果已知,後續程式碼不再執行)
課堂筆記2019.4.15
while語句:列印1-10
1 #列印1=10 2 No = 1 3 4 while No<=10: 5 print(No) 6 No+=1
View Code
課堂筆記2019.4.16
1.編寫一個猜測年齡的程式
1 #猜年輕 2 3 ''' 用if語句判斷 4 goal_age=76 5 6 guess_age=int(input("please guess age(1-100):")) 7 8 # print(guess_age,goal_age) 9 10 if(guess_age==goal_age): 11 print("you got it") 12 else: 13 print("sorry,you are wrong") 14 ''' 15 16 #利用while實現一直輸入 17 ''' 18 暫時無法實現2個問題: 19 1.從輸錯了數字開始算起的區間(比如輸入兩個數字(34,89)後,無法提醒在(34-89)之間的數字猜測) 20 2019.4.22號已自行解決 21 2.由用戶自己選擇放棄猜測退出程式. 2019.5.6 已解決 22 23 ''' 24 goal_age = 76 25 26 guess_age = int(input("please guess age(1-100):")) 27 guess_maxage = 100 28 guess_minage = 1 29 30 while guess_age != goal_age: 31 32 if guess_age < goal_age: # 判斷輸入的數字是否正確 33 print() 34 if guess_age > guess_minage: # 用來取輸入後的最小值 35 guess_minage = guess_age 36 print("your input number is:", guess_age) 37 print("that's too small... please guess ", guess_minage, "- ", guess_maxage, "!!") 38 elif guess_age > goal_age: 39 print() 40 if guess_age < guess_maxage: # 用來取輸入後的最大值 41 guess_maxage = guess_age 42 print("your input number is:", guess_age) 43 print("that's too big... please guess ", guess_minage, " -", guess_maxage, "!!") 44 45 guess_age = input("you can input 'give up' go to out or guess again:") 46 47 if guess_age == "give up": 48 print("It's so pity!!!") 49 break 50 51 guess_age = int(guess_age) 52 53 else: 54 print("you got it")
View Code
2.輸出1-100之間的偶數
1 #輸入1-100之間的偶數 2 3 No=1 4 5 while No<=100: 6 if No%2==0: 7 print(No) 8 No+=1
View Code
3.語法1:break 用來跳出本循環,continue用來結束本次循環。
語法2:print(「abc」,end=「」) 「abc」後面不換行,繼續顯示列印的內容。
語法3:while … else… 非break中止的程式,都會執行else後的程式 。
課堂筆記2019.4.19
編寫九九乘法表
1 ''' 2 個人思路: 3 九九乘法表。 a=1 while a <= 9: b=1 while b<=a:print((b,」*」,a,b*a),end(「,」)) b+=1 a+=1 4 ''' 5 6 high =1 7 8 while high<=9: 9 wieth=1 10 while wieth<=high: 11 print(wieth,"*",high,"=",wieth*high,end="t") # 'n'是換行,'t'是tab 12 wieth+=1 13 print() 14 high+=1
View Code