Python—经典练手题目汇总
Python—经典练手题目汇总
# 1、有1020个西瓜,第一天卖掉总数的一半后又多卖出两个,以后每天卖剩下的一半多两
# 个,问几天以后能卖完?
day=0 xg=1020 for i in range(999): day+=1 xg=xg/2-2 if xg==0: print(day)
# 2、猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一
# 个,第二天早上又将剩下的桃子吃了一半,又多吃一个,以后每天都吃了前一天剩下的
# 一半零一个。到第10天早上想再吃时,见只剩下一个桃子。求第一天共摘了多少个?
peach = 1 for i in range(9): peach = (peach + 1) * 2 print(peach)
# 3、有个人想知道,一年之内一对兔子能繁殖多少对?于是就筑了一道围墙把一对兔子关在
# 里面。已知一对兔子每个月可以生一对小兔子,而一对兔子从出生后第3个月起每月生
# 一对小兔子。假如一年内没有发生死亡现象,那么,一对兔子一年内(12个月)能繁殖
#成多少对?分析:兔子的规律为数列,1,1,2,3,5,8,13,21
one=1 two=1 number=0 for i in range (3,13): number=one+two one=two two=number print(number)
# 4、计算1000以内所有不能被7整除的整数之和
zonghe=0 for i in range( 1001): if i % 7 != 0 : zonghe += i print( zonghe)
# 5、计算1+2-3+4-5+6-7..+100的结果//注意审题
number=1 for i in range(2,101): if i%2==0: number+=i else: number-=i print(number)
# 6、一张纸的厚度大约是0.08mm,对折多少次之后能达到或超过珠穆朗玛峰的高度(8848.13
# 米)
paper=0.00008 cishu=0 for i in range(9999): paper*=2 cishu+=1 if paper>=8848.13: break print(cishu)
# 7、一球从100米高度自由落下,每次落地后反跳回原高度的一
# ;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高
high=100 m=0 for i in range(1,11): m=100/(2**i) if i<10: high=high+(m*2) print("在第十次经过了:",high) print("第十次反弹高度为:",m)
# 8、每个做父母的都关心自己孩子成人后的身高,据有关生理卫生知识与数理统计分析表明,影响小孩成
# 人后身高的因素有遗传、饮食习惯与坚持体育锻炼等。小孩成人后的身高与其父母的身高和自身的性
# 别密切相关
# 设faHeiqht为其父身高,moHeight为其母身高,身高预测公式为:
# 男性成人时身高=(faHeight+moHeiaht*0.54
# 女性成人时身高=(faHeiaht*0.923+moHeiaht)/2
# 此外,如果喜爱体育锻炼,那么可增加身高2%,如果有良好的卫生饮食习惯,那么可增加身高1.5%。
# 利用给定公式和身高预测方法对你的身高进行预测
# 要求父母的身高、是否爱好体育锻炼等参数从键盘输入
SEX=input("请输入您的性别(男|女):") dad_Height=float(input("请输入您父亲身高:")) mami_Height=float(input("请输入您母亲身高:")) PE=input("您是否爱好锻炼(是|否):") EAT=input("您的饮食习惯是否良好(是|否):") a=1 b=1 if PE == "是": a=1.02 if EAT == "是": b=1.015 if SEX == "男": result=(dad_Height + mami_Height) * 0.54 * a *b else: result=(dad_Height * 0.923 + mami_Height) / 2 * a *b print("您的身高是:"+str(result))
#9
# '''定义字典,具有属性账号id,书名name、作者author 和价格price,
# 在创建图书对象时要求通过构造器进行创建,一次性将四个属性全部赋值,
# 1)要求账户属性是int型,名称是String型,作者是String型,价格是double,
# 2)要求定义一个图书馆集合,用于保存多本图书
# 3)在图书馆集合中新增图书,删除图书,修改图书信息
# 4)遍历查看所有添加过的图书
# 5)查询所有图书的平均价格、最高价格、最低价格
#6) 查询作者相同的图书的平均价格'''
dct1={"id":1,"name":"GoldBottleShade","author":"陈寿","price":26.5} dct2={"id":2,"name":"sangozhi","author":"陈寿","price":32.5} dct3={"id":3,"name":"ThreeWomen,HundredAndFiveMan","author":"曹雪芹","price":33.5} dct4={"id":4,"name":"RedBuildingDream","author":"曹雪芹","price":31} dct5={"id":5,"name":"WestTravelStory","author":"吴承恩","price":28.5} lst=[dct1,dct2,dct3,dct4,dct5] print(lst) lst.append({"id":6,"name":"StoryOfGhost","author":"蒲松龄","price":27.0}) #print(lst) # lst.remove(dct5) #删除 # print(dct5) lst[5]["id"] =7 #print(lst) sum=0.0 max=0.0 min=100.0 for x in lst: sum+=x["price"] if max<x["price"]:max=x["price"] if max > x["price"]: max = x["price"] avg=sum/len(lst) print(avg) print(max) print(min) st=set() #从该字典向后比较,遇到名称相同的,把价格加起来,最后平均 for x in range(len(lst)): name=lst[x]["author"] authorPrice=lst[x]["price"] authorCnt=1 for y in range(len(lst)): if name==lst[y]["author"] and x!=y: authorPrice+=lst[y]["price"] authorCnt+=1 else: rst=name+str(authorPrice/authorCnt) st.add(rst) print(st)
#10、'''建立一个列表中,元素是n个字典
[{"name": "zhangsan", "price": 100.5},{"name": "zhangsan", "price": 105.5},
{"name": "zhangsan", "price": 85.5},{"name": "zhangsan", "price": 745},
{"name": "zhangsan", "price": 102}, {"name": "zhangsan", "price": 215},
{"name": "lisi", "price": 253.2}, {"name": "lisi", "price": 25.8},
{"name": "lisi", "price": 85}, {"name": "lisi", "price": 96},
{"name": "lisi", "price": 79.5}, {"name": "wangwu", "price": 122.3},
{"name": "wangwu", "price": 135.5},{"name": "wangwu", "price": 122.3}]
1)计算每个消费者消费的条数
2)计算每个消费者消费的总金额
3)计算每个消费者消费的平均金额
4)计算所有消费者的总金额
5)计算所有消费者的平均金额
'''
lst = [{"name": "zhangsan", "price": 100.5}, {"name": "zhangsan", "price": 105.5}, {"name": "zhangsan", "price": 85.5}, {"name": "zhangsan", "price": 745}, {"name": "zhangsan", "price": 102}, {"name": "zhangsan", "price": 215}, {"name": "lisi", "price": 253.2}, {"name": "lisi", "price": 25.8}, {"name": "lisi", "price": 85}, {"name": "lisi", "price": 96}, {"name": "lisi", "price": 79.5}, {"name": "wangwu", "price": 122.3}, {"name": "wangwu", "price": 135.5}, {"name": "wangwu", "price": 122.3}] result1=set() result2=set() result3=set() countPrice=0 lst2=[] for x in range(len(lst)): priceCNT = lst[x]["price"] a = 1 XFZ = lst[x]["name"] countPrice += lst[x]["price"] lst2.append(lst[x]["price"]) for y in range(len(lst)): if XFZ == lst[y]["name"] and x != y: a +=1 priceCNT+=lst[y]["price"] result1.add((XFZ,a)) result2.add((XFZ,priceCNT)) result3.add((XFZ,priceCNT/a)) print(result1) print(result2) print(result3) print(countPrice) print(lst2) print(countPrice,countPrice/len(result1))