Python課程第一章初探Python習題詳解,python初探
Python課程第一章初探Python習題詳解
1.輸入1-127的ascii碼並輸出對應字元
>>> for i in range(0,127)
... print i,chr(i)
...
0
2. 輸入a,b,c,d4個整數,計算a+b-c*d的結果
>>> a=2
>>> b=3
>>> c=4
>>> d=6
>>> sum=a+b-c*d
>>> print sum
-19
3. 計算一周有多少分鐘、多少秒鐘
>>> weekdays=7
>>> weekdays
7
>>> days=weekdays*7
>>> minutes=days*60
>>> seconds=minutes*60
>>> minutes
2940
>>> seconds
176400
4. 3個人在餐廳吃飯,想分攤飯費。總共花費35.27美元,他們還想給15%的小費。每個人該怎麼付錢,
編程實現
>>> sum=35.27
>>> fee=0.15
>>> cost=sum*(1+fee)
>>> percost=cost/3
>>> percost
13.520166666666666
5. 計算一個12.5m X 16.7m的矩形房間的面積和周長
>>> s=12.5*16.7
>>> s
208.75
>>> l=12.5*2+16.7*2
>>> l
58.4
6. 怎麼得到9 / 2的小數結果
>>> a=9
>>> a/=2
>>> a
4
>>> a=9.0
>>> a/=2
>>> a
4.5
7. python計算中7 * 7 *7 * 7,可以有多少種寫法
>>> a=7
>>> b=pow(7,4)
>>> b
2401
>>> c=7**4
>>> c
2401
>>> d=7*7*7*7
>>> d
2401
8. 寫程式將溫度從華氏溫度轉換為攝氏溫度。轉換公式為C = 5 / 9*(F - 32)
>>> F=int(raw_input("請輸入華氏溫度:"))
請輸入華氏溫度:90
>>> C=(F-32)/(9/5)
>>> C
58
9. 一家商場在降價促銷。如果購買金額50-100元(包含50元和100元)之間,會給10%的折扣,如果
購買金額大於100元會給20%折扣。編寫一程式,詢問購買價格,再顯示出折扣(10%或20%)和
最終價格。
>>> money=int(raw_input("請輸入金額:"))
請輸入金額:90
>>> if money in range(50,101):
... discount=0.1
... finalmoney=money*(1-discount)
... elif money >=100:
... discount=0.2
... finalmoney=money*(1-discount)
...
>>> print finalmoney
81.0
>>> money=int(raw_input("請輸入金額:"))
請輸入金額:90
>>> if money in range(50,101):
... discount=0.1
... finalmoney=money*(1-discount)
... elif money >=100:
... discount=0.2
... finalmoney=money*(1-discount)
...
>>> print finalmoney
81.0
>>> print discount
0.1
10. 判斷一個數n能否同時被3和5整除
>>> n=int(raw_input("判斷一個數n能否同時被3和5整除:"))
判斷一個數n能否同時被3和5整除:89
>>> if n%3==0 and n%5==0:
... print "%s" %"n能3和5整除"
... else:
... print "%s" %"n不能3和5整除"
...
n不能3和5整除
>>>
11.求1 + 2 + 3 +….+100
>>> sum=0
>>> for i in range(1,101):
... sum+=i
>>> print sum
5050
12交換兩個變數的值
>>> a=12
>>> b=34
>>> a=b
>>> a
34
>>> b=a
>>> a,b=12,34
>>> b,a=a,b
>>> b
12
>>> a
34
>>>
...
13一個足球隊在尋找年齡在10到12歲的小女孩(包括10歲和12歲)加入。編寫一個程式,詢問使用者的性別(m表示男性,
f表示女性)和年齡,然後顯示一條訊息指出這個人是否可以加進球隊,詢問10次後,輸出滿足條件的總人數。
>>>i=0
>>> while i<=10:
... sex=raw_input("please input sex:")
... age=int(raw_input("please input age:"))
... i+=1
... if sex=="female":
... if age>=10 and age<=12:
... print u"這個女孩可以加進球隊"
... sum+=1
... elif age<10 and age<12:
... print u"這個女孩的年齡不符合要求"
... elif sex=="male":
... print u"我們要求性別是女孩"
>>>print u"滿足條件的孩子有%s" %sum
14 長途旅行中,剛到一個加油站,距下一個加油站還有200km,而且以後每個加油站之間距離都是200km。編寫一個程
序確定是不是需要在這裡加油,還是可以等到接下來的第幾個加油站再加油。
程式詢問以下幾個問題:
1)你車的油箱多大,單位升
2)目前油箱還剩多少油,按百分比算,比如一半就是0.5
3)你車每升油可以走多遠(km)
提示:
油箱中包含5升的緩衝油,以防油表不準。
>>> car_gas_volume=200
>>> car_current_gas_volume=150
>>> car_100_gas_consume=10
>>> car_gas_gap_distance=200
>>> print u"請在第%s個加油站加油" %int((car_current_gas_volume-5)/car_100_gas_co
nsume*100/car_gas_gap_distance)
請在第7個加油站加油
15 現有麵包、熱狗、番茄醬、芥末醬以及洋蔥,數字顯示有多少種訂購組合,其中麵包必訂,0不訂,1訂,比如10000,
表示只訂購麵包
>>> bread=["1"]
>>> hotdog=["0","1"]
>>> mustard=["0","1"]
>>> onion=["0","1"]
>>> combination=[]
>>> for b in bread:
... for h in hotdog:
... for t in tomato_jam:
... for m in mustard:
... for o in onion:
... combination.append(b+h+t+m+o)
...
>>> print combination
['10000', '10001', '10010', '10011', '10100', '10101', '10110', '10111', '11000'
, '11001', '11010', '11011', '11100', '11101', '11110', '11111']
>>> for per_combination in combination:
... print per_combination
...
10000
10001
10010
10011
10100
10101
10110
10111
11000
11001
11010
11011
11100
11101
11110
11111
16 基於上題:給出每種食物的卡路裡(自訂),再計算出每種組合總共的卡路裡
>>> bread_carolie=1
>>> tomato_jam_carolie=1
>>> mustard_carolie=1
>>> onion_carolie=1
>>>>>> for per_combination in combination:
... calorie_count=0
... for category in per_combination.split():
... if category[0]=="1":
... calorie_count+=bread_carolie
... if category[1]=="1":
... calorie_count+=tomato_jam_carolie
... if category[2]=="1":
... calorie_count+=mustard_carolie
... if category[3]=="1":
... calorie_count+=onion_carolie
... print category,"組合的卡路裡總和是:%d" %carolie_count,u"千卡"
...
10000 組合的卡路裡總和是:1 千卡
10001 組合的卡路裡總和是:1 千卡
10010 組合的卡路裡總和是:2 千卡
10011 組合的卡路裡總和是:2 千卡
10100 組合的卡路裡總和是:2 千卡
10101 組合的卡路裡總和是:2 千卡
10110 組合的卡路裡總和是:3 千卡
10111 組合的卡路裡總和是:3 千卡
11000 組合的卡路裡總和是:2 千卡
11001 組合的卡路裡總和是:2 千卡
11010 組合的卡路裡總和是:3 千卡
11011 組合的卡路裡總和是:3 千卡
11100 組合的卡路裡總和是:3 千卡
11101 組合的卡路裡總和是:3 千卡
11110 組合的卡路裡總和是:4 千卡
11111 組合的卡路裡總和是:4 千卡
17輸入5個名字,排序後輸出
>>> name=["Job","Ruth","David","Tom","Renee"]
>>> name.sort()
>>> name
['David', 'Job', 'Renee', 'Ruth', 'Tom']
>>> sorted(name)
['David', 'Job', 'Renee', 'Ruth', 'Tom']
>>> sorted(name,reverse=true)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined
>>> sorted(name,reverse=True)
['Tom', 'Ruth', 'Renee', 'Job', 'David']
18實現一個簡單的單詞本
功能:
可以添加單詞和詞義,當所添加的單詞已存在,讓使用者知道;
可以尋找單詞,當尋找的單詞不存在時,讓使用者知道;
可以刪除單詞,當刪除的單詞不存在時,讓使用者知道;
以上功能可以無限制操作,直到使用者輸入bye退出程式。
>>> dic={}
>>> help='''
... 1:add a word
... 2:find a word
... 3:delete a word
... 4:say byebye'''
>>> while 1:
... command=int(raw_input("please input a command:"))
... if command==1:
... word=raw_input("please input a word what you want to add:")
... if dic.has_key(word):
... print "the word has existed!"
... continue
... dic[word]=word
... if command==2:
... word=raw_input("please input a word what you want to find:")
... if dic.has_key(word):
... print dic[word]
... continue
... print "the word is not found!"
... if command==3:
... word=raw_input("please input a word what you want to delete:")
... if dic.has_key(word):
... del dic[word]
... print "deletion is done!"
... continue
... print "the word what you want to add is not found!"
... if command==4:
... print "say byebye"
... break
...
please input a command:1
please input a word what you want to add:K
please input a command:1
please input a word what you want to add:K
the word has existed!
please input a command:2
please input a word what you want to find:L
the word is not found!
please input a command:3
please input a word what you want to delete:K
deletion is done!
please input a command:4
say byebye
19輸入一個正整數,輸出其階乘結果
>>> n=int(raw_input("please input a number,calcuate it's factor:"))
please input a number,calcuate it's factor:3
>>> factor=1
>>> for i in range(1,n+1):
... factor=factor*i
...
2
6
20 計算存款利息
4種方法可選:
活期,年利率為r1;
一年期定息,年利率為r2;
存兩次半年期定期,年利率為r3
兩年期定息,年利率為r4
現有本金1000元,請分別計算出一年後按4種方法所得到的本息和。
提示:本息= 本金+ 本金* 年利率* 存款期
>>> r1=0.0035
>>> r2=0.035
>>> r3=0.0155
>>> r4=0.04
>>> deposit=1000
>>> print u"一年期活期總收益:%s" %(deposit*(1+r1))
一年期活期總收益:1003.5
>>> print u"一年期定期總收益:%s" %(deposit*(1+r2)
... )
一年期定期總收益:1035.0
>>> print u"半年期定期總收益:%s" %(deposit*(1+r3/2))
半年期定期總收益:1007.75
>>> print u"兩年期定期總收益:%s" %(deposit*((1+r4)**2))
兩年期定期總收益:1081.6
21
輸入3個數字,以逗號隔開,輸出其中最大的數
a=[1,2,3]
>>> max(a)
3
22輸入一個年份,輸出是否為閏年
是閏年的條件:
能被4整數但不能被100整除,或者能被400整除的年份都是閏年。
>>> leapyear=int(raw_input("pleas input leap year:"))
pleas input leap year:1984
>>> if leapyear%4==0 and leapyear%100!=0:
... print ("%d is leap year" %leapyear)
... elif leapyear%400==0:
... print ("%d is also leap year" %leapyear)
... else:
... print ("%d is not leap year" %leapyear)
...
1984 is leap year
23求兩個正整數m和n的最大公約數
>>> a=int(raw_input("please input A number:"))
please input A number:16
>>> 16
16
>>> b=int(raw_input("please input B number:"))
please input B number:24
>>> su=[]
>>> for i in range(1,smaller+1):
... if a%i==0 and b%i==0:
... su.append(i)
...
>>> l=len(su)
>>> p=su[l-1]
>>> su
[1, 2, 4, 8]
>>> p
8
>>> a=int(raw_input("please input A number:"
please input A number:16
>>> 16
16
>>> b=int(raw_input("please input B number:"))
please input B number:24
>>> su=[]
>>> for i in
>>> for i in range(1,bigger+1):
... if a%i==0 and b%i==0:
... su.append(i)
...
>>> su
[1, 2, 4, 8]
>>> l=len(su)
>>> p=su[l-1]
>>> p
8