大家在做第一章測試題時,需要復習如下相關知識點:編譯型VS解釋型、變量名規范、數據類型、程序交互、格式化輸出、運算符、流程控制。
1.簡述編譯型與解釋型語言的區別,且分別列出你知道的那些語言屬于編譯型,哪些屬于解釋型。
2.執行Python腳本的兩種方式是什么?
3.布爾值分別有什么?
4.如何查看變量在內存中的地址?
5.實現用戶輸入用戶名和密碼,當用戶名為seven且密碼為123時,顯示登錄成功,否則登錄失敗!


1 user = 'seven' 2 pwd = 123 3 username = input('username:') 4 password = int(input('password:')) 5 if username == user and password == pwd: 6 print('Welcome to %s' % username) 7 else: 8 print('Wrong username or password')
6.實現用戶輸入用戶名和密碼,當用戶名為seven且密碼為123時,顯示登錄成功,否則登錄失敗,失敗時允許重復輸入三次。


1 user = 'seven' 2 pwd = 123 3 count = 0 # 計數器 4 while count < 3: 5 username = input('username:') 6 password = int(input('password:')) 7 if username == user and password == pwd: 8 print('Welcome to %s' % username) 9 break 10 else: 11 print('Wrong username or password') 12 count += 1
7.實現用戶輸入用戶名和密碼,當用戶名為seven或Alex且密碼為123時,顯示登錄成功,否則登錄失敗,失敗時允許重復輸入三次。


1 user = ['seven', 'alex'] 2 pwd = 123 3 count = 0 4 while count < 3: 5 username = input('username:') 6 password = int(input('password:')) 7 if username in user: 8 if password == pwd: 9 print('Welcome to %s' % username) 10 break 11 else: 12 print('Wrong username or password') 13 count += 1
8.聲明變量注意事項有哪些?
9.Python 單行注釋和多行注釋分別用什么?
10.編寫成績的小程序,成績有ABCDE5個等級,與分數的對應關系如下:
A 90-100
B 80-89
C 60-79
D 40-59
E 0-39
要求:用戶輸入0-100的數字后,你能正確打印它的對應成績


1 grade = int(input('please your grade:')) 2 if grade >= 100: 3 print('grade is 0-100') 4 elif 90 <= grade < 100: 5 print('A') 6 elif 80 <= grade < 89: 7 print('B') 8 elif 60 <= grade < 79: 9 print('C') 10 elif 40 <= grade < 59: 11 print('D') 12 else: 13 print('E')
11.寫代碼
a.使用while循環實現輸出2-3+4-5+6...+100的和


1 count = 2 2 s = 0 3 while count <= 100: # loop 3 s = -1 count = 4 4 if count % 2 == 0: 5 s += count 6 else: 7 s -= count # 3 8 count += 1 9 print(s)
b.使用while循環實現輸出1,2,3,4,5,8,9,11,12


1 count = 0 2 while count < 12: 3 count += 1 4 if count == 6: 5 continue 6 elif count == 10: 7 continue 8 print(count)
c.使用while循環輸出100-50,從大到小,如100,99,98...,到50時,再從0循環輸出到50,然后結束。
小白方法:


1 count = 100 2 while count >= 50: 3 print(count) 4 count -= 1 5 count = 0 6 while count <= 50: 7 print(count) 8 count += 1
高級玩法:


1 count = 0 2 while count <= 100: 3 count += 1 4 if count <= 50: 5 print(100 - count) # count = 1 6 else: 7 print(count - 51) # count =51 0 52 1
d. 使用while 循環實現輸出1-100內的所有奇數。


1 count = 0 2 while count < 100: 3 count += 1 4 if count % 2 != 0: 5 print(count)
e.使用while循環實現輸出1-100內的所有偶數。


1 count = 0 2 while count < 100: 3 count += 1 4 if count % 2 == 0: 5 print(count)
12.制作趣味模板程序(編程題)
需求:等待用戶輸入名字、地點、愛好,根據用戶的名字和愛好任意顯示。


1 name = input("Name:").strip() 2 site = input("Site:").strip() 3 hobby = input("Hobby:").strip() 4 print("可愛的%s,最喜歡在%s看書。愛好是%s" % (name, site, hobby))
13.輸入年份,判斷該年份是否是閏年并輸出結果。(編程題)
需求:凡符合下面兩個條件之一的年份就是閏年。(1)能被4整除但不能被100整除。(2)能被400整除。


1 while True: 2 year = int(input('Please is year:')) 3 if year % 4 == 0 and year % 100 != 0: 4 print("%s is leap year." % year) 5 break 6 elif year % 400 == 0: 7 exit("%s is a century" % year) 8 else: 9 exit("不是閏年")
?14.假設一年定期利率為3.25%,計算一下要過多少年,一萬元的一年定期存款連本帶息能翻翻?(編程題)


1 # 利息計算的基本公式為:利息=本金×存期時間×存款利率; 2 capital = 10000 # 本金 3 count = 0 4 while capital< 20000: 5 count += 1 6 interest = capital * 0.0325 # 利息 7 capital += interest # 連本帶息 8 print(capital, count)
15.使用while,完成以下圖形的輸出
*
**
***
****
*****
****
***
**
*


1 count = 0 2 while count < 10: 3 count += 1 4 if count <= 5: 5 print(count * '*') 6 else: 7 print((10-count) * '*') # count 6
16.使用while循環實現1-100的整數相加


1 count = 0 2 s = 0 3 while count < 100: 4 count += 1 5 s += count 6 print(s)
17.一球從100米高度自由落下。每次落地后反跳回原高度的一半;在落下,求它在第10次落地時,共經過多少米?第10次反跳多高。


1 height = 100 2 count = 0 3 n = 100 4 while count < 10: 5 height = height / 2 # 反彈高度50, 6 n += height * 2 # 統計總長度 7 count += 1 8 print(count, height, n)
?18.表達式for loop
最簡單的循環10次


1 for i in range(10): 2 print('loop', i)
需求一:還是上面的程序,但是遇到小于5的循環次數就不走了,直接跳入下一次循環


1 for i in range(10): 2 if i < 5: 3 continue # 不往下走了,直接進入下一次loop 4 print('loop', i)
需求二:還是上面的程序,但是遇到大于5的循環次數就不走了,直接退出


1 for i in range(10): 2 if i > 5: 3 break # 不往下走了,直接跳出整個loop 4 print('loop', i)
?