Python——第三天的Bleeding Jane

作业回顾01:

'''    课堂练习
1.年龄在 22 到 35岁之间 打印可以结婚 在其他年龄段 枪毙
2.年龄在70岁以上 或者 患有老年痴呆 打印不能开车,否则可以开车
'''

# # 练习1
# age = int(input("请输入一个年龄:"))
# if age >= 22 and age < 35:
#     print("结婚年龄")
# else:
#     print("枪毙")

# 练习2
age = int(input("请输入一个年龄:"))
sick = input("请输入是否有:老年痴呆(是输入1,不是输入0)")
# or 只要满足一个条件就执行
if age >= 70 or sick == "1":
    print("不能开车")
else:
    print("可以开车")

运行结果:
在这里插入图片描述
作业回顾02:

'''
随堂练习
1,
*
**
***
****
2,
*
***
*****
'''

#练习1
i = 1
while i <= 5:
    print("*"*i)
    i += 1
print("=================================")

#练习2
# j = 1
# while j <= 5:
#     print("*" * j)
#     j += 2

#第二种写法
j = 1
while j <= 5:
    # 取余操作 当j是奇数的时候 执行打印
    if j % 2 == 1:
        print("*" * j)
    j += 1

运行结果:
在这里插入图片描述
Python的while循环:

# 使用while实现0-100的相加
i = 0
j = 0
while j < 100:
    i = i+j

    j += 1
    print(i,"+",j,"=",j+i)

'''
随堂练习:
1、将0-100所有3的倍数相加
2、将0-100所有3的倍数相加 含有3的 相加
3、奇数排序思想:先判断十位数上的大小,再排个位数上的大小
46,37,42
37,46,42
37,42,46
使用奇数排序思想给上面三个数进行排序,从大到小 打印出来
a = 46,b = 37,c = 42
4、归并排序
a = 46,b = 37, c = 42,d = 39
把ab放在一组 把cd放在一组
ab选出较大,cd选出较大值,然后 再把ac放在一起比较,选出谁是最大 打印出来
同样的方法 选出最小 打印出来
'''
i = 0
sum = 0
while i <= 100:
    if i % 3 == 0:
        print(i)
        sum += i
    i += 1
print(sum)

print("================================")

运行结果:

0 + 1 = 1
1 + 2 = 3
3 + 3 = 6
6 + 4 = 10
10 + 5 = 15
15 + 6 = 21
21 + 7 = 28
28 + 8 = 36
36 + 9 = 45
45 + 10 = 55
55 + 11 = 66
66 + 12 = 78
78 + 13 = 91
91 + 14 = 105
105 + 15 = 120
120 + 16 = 136
136 + 17 = 153
153 + 18 = 171
171 + 19 = 190
190 + 20 = 210
210 + 21 = 231
231 + 22 = 253
253 + 23 = 276
276 + 24 = 300
300 + 25 = 325
325 + 26 = 351
351 + 27 = 378
378 + 28 = 406
406 + 29 = 435
435 + 30 = 465
465 + 31 = 496
496 + 32 = 528
528 + 33 = 561
561 + 34 = 595
595 + 35 = 630
630 + 36 = 666
666 + 37 = 703
703 + 38 = 741
741 + 39 = 780
780 + 40 = 820
820 + 41 = 861
861 + 42 = 903
903 + 43 = 946
946 + 44 = 990
990 + 45 = 1035
1035 + 46 = 1081
1081 + 47 = 1128
1128 + 48 = 1176
1176 + 49 = 1225
1225 + 50 = 1275
1275 + 51 = 1326
1326 + 52 = 1378
1378 + 53 = 1431
1431 + 54 = 1485
1485 + 55 = 1540
1540 + 56 = 1596
1596 + 57 = 1653
1653 + 58 = 1711
1711 + 59 = 1770
1770 + 60 = 1830
1830 + 61 = 1891
1891 + 62 = 1953
1953 + 63 = 2016
2016 + 64 = 2080
2080 + 65 = 2145
2145 + 66 = 2211
2211 + 67 = 2278
2278 + 68 = 2346
2346 + 69 = 2415
2415 + 70 = 2485
2485 + 71 = 2556
2556 + 72 = 2628
2628 + 73 = 2701
2701 + 74 = 2775
2775 + 75 = 2850
2850 + 76 = 2926
2926 + 77 = 3003
3003 + 78 = 3081
3081 + 79 = 3160
3160 + 80 = 3240
3240 + 81 = 3321
3321 + 82 = 3403
3403 + 83 = 3486
3486 + 84 = 3570
3570 + 85 = 3655
3655 + 86 = 3741
3741 + 87 = 3828
3828 + 88 = 3916
3916 + 89 = 4005
4005 + 90 = 4095
4095 + 91 = 4186
4186 + 92 = 4278
4278 + 93 = 4371
4371 + 94 = 4465
4465 + 95 = 4560
4560 + 96 = 4656
4656 + 97 = 4753
4753 + 98 = 4851
4851 + 99 = 4950
4950 + 100 = 5050
0
3
6
9
12
15
18
21
24
27
30
33
36
39
42
45
48
51
54
57
60
63
66
69
72
75
78
81
84
87
90
93
96
99
1683
================================

进程已结束,退出代码0

Python的break终止循环:

#吃四给苹果
i = 1
while i <= 4:
    print(f"吃第{i}个苹果")
    i += 1
print("================================")
#吃四给苹果 吃到第三个我就饱了 不用吃第四个了
i = 1
while i <= 4:
    if i == 3:
        print(f"吃第{i}个苹果")
        print("我吃饱了,毁灭吧,结束吧")
        #break是终止所有循环
        break
    i += 1
print("=================================")

运行结果:
在这里插入图片描述
Python的 continue 跳出本次循环:

#第三给循环 吃到一个带有虫子的苹果 跳出本次循环 继续吃第四个苹果 第三给苹果就不吃
i = 0
while i <= 4:
    i += 1
    if i == 3:
        print("这个苹果有虫子,扔掉,退出本次循环")

        continue
    print(f"吃第{i}个苹果")
#break和continue的区别,break搜索终止所有循环,continue是跳出本次循环

运行结果:
在这里插入图片描述
Python的 for 循环:

#循环打印python每个单词
str1 ="python"
print(str1[3])
#i 是临时变量,用来取值
for i in str1:
    print(i)
print("==========================")
'''
随堂练习
1,使用while循环取出y和o,打印出来
'''
j = 0
while j < 6:
    j +=1
    if j == 1 or j ==4:
        print(str1[j])
print("==========================")
#使用for循环 打印James 跳过m 打印后面的内容
for i in "James":
    if i == "m":
        continue
    print(i,end=' ')

运行结果:
在这里插入图片描述
简易的猜拳游戏(随机运用):

'''
石头(1) 剪刀(2) 布(3)
电脑赢:
'''
import  random
user = int(input("请输入猜拳数字石头(1)剪刀(2)布(3)"))
#computer是一个int类型
computer = random.randint(1,3)
print(computer,"-",user)

if user == computer:
    print("平局")
elif (computer == 1 and user == 3) or (user == 1 and computer == 2) or (computer == 3 and user == 2):
    print("你赢了")
else :
    print("我赢了")

输入数字与计算机随机出的数做比较来决定结果
过程的不同决定不同的随机性结局
Python的 randge 的用法:

i = 1
while i <=5:
    print(i)
    i += 1
print("===============================")
for i in range(1,6):
    print(i)
print("===============================")
for i in range(1,6):
    print("*" * i)
print("===============================")
for i in range(1,6):
    if i % 2 == 1:
        print("*" * i)

运行结果:

1
2
3
4
5
===============================
1
2
3
4
5
===============================
*
**
***
****
*****
===============================
*
***
*****

进程已结束,退出代码0

简化代码的好帮手?

Python的字符串操作01:

#定义一个字符串 字符串使用单引号或双引号定义
a = "Hello Python"
b = 'abcdef'
print(type(a))
print(type(b))

name1 = '秦晓天'
name2 = "范顺"
name3 = '''廖警官'''
name4 = """胡航"""
name5 = '''汤佳亮'''

print(type(name1))
print(type(name2))
print(type(name3))
print(type(name4))
print(type(name5))
#格式化字符串
print(f"你的名字是{name1}")

print("=================================")

# 01234 字符串下标 或者 叫做 索引
name = "simon"
print(name[0],end=" ") # 取s
print(name[2],end=" ") # 取m
print(name[4],end=" ") # 取n

print("=================================")

# 切片    0123456
name6 = "abcdefg"
# 第一个数字是开始位置(包含)
# 第二个位置上的是结束位置(不包含)
#要前不要后
print(name6[2:5])
print(name6[2:5:1])    #效果同上 第三个参数默认是1
print(name6[:5])       #第一个参数不写 默认是0
print(name6[4:])       #第二个参数不写 默认取到最后
print(name6[:])        #第一个和第二个参数都不写 表示取整个字符串 从头取到尾
print(name6[::2])      #步长是2 表示跳着取 0246位置上的值
print(name6[-1])
print(name6[:-1])      #从开始取到 最后一个  但最后一个值不要
print(name6[-4:-2])    #把倒数第四和第三取出来
print(name6[::-1])     #把字符串倒过来

print("=================================")

#字符串查找 find
#         0123456789
mystr = "hello world"
# 查找hello 差找到了返回 子串在原来位置中的起始位置
result1 = mystr.find("hello")
print(result1)

result2 = mystr.find("world")
print(result2)

#在字符串中查找一个不存在的值 查找失败 返回-1
result3 = mystr.find("python")
print(result3)

result4 = mystr.find("lo", 6, 10) #在位置6-10查找不到lo
print(result4)

result5 = mystr.find("lo", 2, 6)
print(result5)

print("=================================")

# 字符串查找index
mystr1 = "hello world"
#查找到则返回 第一个字符的位置下标
ret1 = mystr1.index("hello")
print(ret1)
ret2 = mystr1.index("world")
print(ret2)
# ValueError: substring not found
# 表示没有找到 则报错 报错会导致程序结束
# ret3 = mystr1.index("python")
# print(ret3)
#在指定位置上查找
# ret4 = mystr1.index("lo", 4, 8)
# print(ret4)
ret5 = mystr1.index("lo", 1, 8)
print(ret5)

运行结果:

<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
你的名字是秦晓天
=================================
s m n =================================
cde
cde
abcde
efg
abcdefg
aceg
g
abcdef
de
gfedcba
=================================
0
6
-1
-1
3
=================================
0
6
3

进程已结束,退出代码0

Python的字符串操作02:


#统计字符串次数count

com_apply = "This class is very good, so good, people good, so amazing"
#计算good在字符串中的次数
result1 = com_apply.count("good")
print(result1)
#24 , 50是指定范围内 查找good 的参数
result2 = com_apply.count("good", 24,50)
print(result2)

print("=================================")

com_apply = "This class is very good, so good, people good, so amazing"
#将所有的good替换为bad 生成一个新的字符串 原来字符串没有变化 第三个参数是替换的次数
#第三个参数是替换次数
result3 = com_apply.replace("good","bad",2)
print(result3)
print(com_apply)

运行结果:
在这里插入图片描述
Bleeding Jane是由1 Bronson Extract、3 Powdered Delta和3 Flanergide调和而成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值