(自学#python)Day05-了解python的循环语句

(自学python)Day05-了解python的循环语句

1、while循环

(1)while循环配合if语句使用

"""
    while 循环
        延长程序生命
        while True:
            循环体
            if 条件:
                break
"""
while True:
    if input("请输入性别:") == "男":
        print("您好先生")
    else:
        print("您好女士")
    if input("请输入q键退出:") == "q":
        break # 结束循环
"""
练习1:
让下列代码重复执行,输入y继续(不输入y则退出)
number = int(input("请输入数字:"))
if number > 0:
    print("正数")
elif number < 0:
    print("负数")
else:
    print("零")
"""
while True:
    number = int(input("请输入数字:"))
    if number > 0:
        print("正数")
    elif number < 0:
        print("负数")
    else:
        print("零")
    if input("请输入y键继续:") != "y":
        break

(2)while 循环计数

"""
    while 循环计数
        三要素:
            开始,结束,间隔
            
	while 循环语法
        开始
        while 结束:
            间隔

"""
count = 0  # 开始
while count < 5:  # 结束
    print(count)  # 0~4
    count += 1  # 间隔
"""
练习2:
在终端中显示0 1 2 3
在终端中显示2 3 4 5 6
在终端中显示1 3 5 7
在终端中显示8 7 6 5 4
在终端中显示-1 -2 -3 -4 -5
"""
count = 0
while count < 4:
    print(count)
    count += 1

count = 2
while count < 7:
    print(count)
    count += 1

count = 1
while count < 8:
    print(count)
    count += 2

count = 8
while count > 3:
    print(count)
    count += -1

count = -1
while count > -6:
    print(count)
    count += -1

"""
    练习3:
    在终端中循环录入5个成绩,
    最后打印平均成绩(总成绩除以人数)
    效果:
    请输入成绩:98
    请输入成绩:83
    请输入成绩:90
    请输入成绩:99
    请输入成绩:78
    平均分:89.6
"""
count = 0
total_score = 0
while count < 5:
    total_score += float(input("请输入成绩:"))
    count += 1
print("平均成绩:"+str(total_score / count))
"""
    练习4:
    一张纸的厚度是0.01毫米
    请计算,对折多少次超过珠穆朗玛峰(8844.43米)
    思路:
    数据:厚度、高度、次数
    算法:厚度*=2      次数+=1
"""
# thickness = 0.01 / 1000
# thickness = 0.00001
thickness = 1e-5
count = 0
while thickness < 8844.43:
    thickness *= 2
    count += 1
print("总共需要对折" + str(count) + "次")
"""
    练习5:
    程序产生1个,1到100之间的随机数。
    让玩家重复猜测,直到猜对为止。
    每次提示:大了、小了、恭喜猜对了,总共猜了多少次。
"""
# 准备随机数工具
import random

# 产生一个随机数
random_number = random.randint(1, 100)
count = 0
while True:
    count += 1
    input_number = int(input("请输入数字:"))
    if input_number > random_number:
        print("大了")
    elif input_number < random_number:
        print("小了")
    else:
        print("恭喜猜对了,总共猜了" + str(count) + "次。")

2、for循环

(1)for循环语法

"""
    for 循环
        拿
            for item in 容器:
                循环体
"""
message = "我爱python编程"
for item in message:
    print(item)
"""
    练习1:
    在终端中输入任意整数,计算累加和.
    "1234" -> "1" -> 累加 1
"""
sum_value = 0
for item in input("请输入任意整数:"):
    sum_value += int(item)
print(sum_value)

(2)for+ range()

"""
    for + range()
        一个范围的整数
"""
# 写法1:range(开始,结束,间隔)
# 注意:不包含结束
for item in range(1,5,2):# 1 3
    print(item)
# 写法2:range(开始,结束,间隔)
# 注意:结束默认为1
for item in range(1,5):# 1~4
    print(item)
# 写法3:range(开始,结束,间隔)
# 注意:开始默认为0
for item in range(5):# 0~4
    print(item)
"""
    练习:
    在终端中累加 0 1 2 3
    在终端中累加 2 3 4 5 6
    在终端中累加 1 3 5 7
    在终端中累加 8 7 6 5 4
    在终端中累加 -1 -2 -3 -4 -5
"""
total_value = 0
for item in range(4):
    total_value += item
print(total_value)

total_value = 0
for item in range(2, 7):
    total_value += item
print(total_value)

total_value = 0
for item in range(1, 8, 2):
    total_value += item
print(total_value)

total_value = 0
for item in range(8, 3, -1):
    total_value += item
print(total_value)

total_value = 0
for item in range(-1, -6, -1):
    total_value += item
print(total_value)

(3)掌握两种编程思想,区分continue和break的作用

# 需求:累加1-100整数
# 条件:能被3整除
# 思想1:满足条件,则累加
sum_value = 0
for item in range(1, 101):
     if item % 3 == 0:
         sum_value += item
 print(sum_value)

# 思想2:不满足条件跳过,否则累加
sum_value = 0
for item in range(1, 101):  # 嗑100个瓜子
    if item % 3 != 0:  # 如果遇到坏的:
        continue  # 跳过  # 吐出去,继续磕
        # break # 跳出 # 吐出去,不吃了
    sum_value += item
print(sum_value)
"""
练习:累加10 -- 60之间,个位不是3/5/8的整数和。
思想1:不是3/5/8 则累加
思想2:是3/5/8 则跳过
"""
# 思想1:
total_value = 0
for item in range(10, 61):
    # if item % 10 != 3 and item % 10 != 5 and item % 10 != 8:
    unit = item % 10
    if unit != 3 and unit != 5 and unit != 8:
        total_value += item
print(total_value)
# 思想2:
total_value = 0
for item in range(10, 61):
    unit = item % 10
    if unit == 3 or unit == 5 or unit == 8:
        continue
    total_value += item
print(total_value)

3、小结

"""
    小结-循环
        while
            根据条件重复,例如:纸张对折超过珠穆朗玛峰
        for
            取出容器元素,例如:获取字符串中每个字
        for + range
            根据次数重复,例如:纸张对折十次
"""
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值