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