接触Python day2

接触Python day2,第1张

接触Python day2

昨天懒了一天看了一天的韩剧太颓废了,今天继续把没搞完的搞完

实战小程序

存款(自己写)七年计算利润的公式写错了

#存款多少年才能翻倍
money=10000
pro=0.0325
for i in range(10000):
    money=money*(1+pro)**i
    if money>=20000:
        print(i)
        break

存款(答案)

base=10000
interest=0.0325
year=0

while base<=20000:
    base = base +(base*interest)
    year=year+1

print(f"需要{year}年,能取出{base}")
需要22年,能取出20210.698678761957

Process finished with exit code 0

小球坠落

自己写

#小球坠落长度计算
height=100
count=0
s=0
while count<=10:
    height=height*(0.5**count)
    s=height+s
    count+=1

print(f"第十次反弹完之后小球经过了{s}米")

第十次反d完之后小球经过了164.16325606551538米

Process finished with exit code 0

答案(少考虑了一个过程) 

#小球坠落长度计算
height=100
count=0
s=0
while count<10:
    s = height + s#坠落时经过的长度
    height=height*0.5
    s = height + s#反弹时经过的长度
#有两个过程 先坠落在反弹 两个过程经过的长度
    count+=1

    print(s,height)

 

150.0 50.0
225.0 25.0
262.5 12.5
281.25 6.25
290.625 3.125
295.3125 1.5625
297.65625 0.78125
298.828125 0.390625
299.4140625 0.1953125
299.70703125 0.09765625

Process finished with exit code 0

 猴子吃桃

自己写

#猴子吃桃
#猴子每天吃桃子种数的一半并多吃一个,吃了十天,到第11天只剩一个桃子。求一共有几个桃子
a=1
day=10
while day>0:
    a=(a+1)*2
    day-=1
    print(day,a)



9 4
8 10
7 22
6 46
5 94
4 190
3 382
2 766
1 1534
0 3070

Process finished with exit code 0

写对了

#计算1-2+3-4+5.....100
a=0
for i in range(1,101):
    j=-(i+1)
    a=a+i+j
    print(a)


-1
-2
-3
-4
-5
-6
-7
-8
-9
-10
-11
-12
-13
-14
-15
-16
-17
-18
-19
-20
-21
-22
-23
-24
-25
-26
-27
-28
-29
-30
-31
-32
-33
-34
-35
-36
-37
-38
-39
-40
-41
-42
-43
-44
-45
-46
-47
-48
-49
-50
-51
-52
-53
-54
-55
-56
-57
-58
-59
-60
-61
-62
-63
-64
-65
-66
-67
-68
-69
-70
-71
-72
-73
-74
-75
-76
-77
-78
-79
-80
-81
-82
-83
-84
-85
-86
-87
-88
-89
-90
-91
-92
-93
-94
-95
-96
-97
-98
-99
-100

Process finished with exit code 0

 

#寻找列表的最大值最小值

我不会写 最头疼的就是这种比大小的问题了,我感觉我逻辑超差

呜呜呜

#寻找列表的最大值最小值
data=[1,87,23,54,74,654,86,23,22,94,54,654,535,53,75,]
max_n=data[0]#假设第一个至最大 第一次知道居然可以这么写 将变量赋予列表的值,
for i in data:#直接用i来表示列表里的第几个书
    if i >max_n:#先判断谁大谁小
        max_n=i#再交换他们的值
print(max_n)


654

Process finished with exit code 0

寻找组合

自己写

#寻找组合
#从两个列表里各取出一个数,如果这两个数的和等于10,则以列表的方式输出这两个数
data=[9,10,26,17,33,3,5,18,34,4,32,25,2,11]
data2=[8,3,2,1,-5,19,2,4,6,7,11]
i=data[0]
j=data2[0]
for i in data:
    for j in data2:
        if i+j==10:
            print([i,j])


[9, 1]
[3, 7]
[4, 6]
[2, 8]

Process finished with exit code 0

写对了

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5710724.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存