python3基础之while死循环,while计数

while循环

1.语法:
while 条件:
满足条件一直执行
break

break 跳转语句

2.死循环:

循环条件永远是满足的

while True:
    usd = int(input("请输入美元:"))
    print(usd * 6.9)
    if input("输入q键退出:"):
        break # 退出循环体
        

输出结果
请输入美元:8
55.2
输入q键退出:
请输入美元:5
34.5
输入q键退出:q

练习1.使下列代码循环执行,按e键退出

season = input("请输入季节:")
if season == "春":
    print("1,2,3月")
elif season == "夏":
    print("4,5,6月")
elif season == "秋":
    print("7,8,9月")
elif season == "冬":
    print("10,111,12月")

# 改写如下
while True:
    season = input("请输入季节:")
if season == "春":
    print("1,2,3月")
elif season == "夏":
    print("4,5,6月")
elif season == "秋":
    print("7,8,9月")
elif season == "冬":
    print("10,111,12月")
if input("输入e键退出")
    break

# 代码改写如下
while True:
    season = input("请输入季节:")
    if season == "春":
        print("1,2,3月")
    elif season == "夏":
        print("4,5,6月")
    elif season == "秋":
        print("7,8,9月")
    elif season == "冬":
        print("10,111,12月")
    if input("输入e键退出:") == "e":
        break

输出结果
请输入季节:夏
4,5,6月
输入e键退出:e

while 计数

#需求:执行三次
count = 0
while count < 3: # 0,1,2
    count += 1
    usd = int(input("请输入美元:"))
    print(usd * 6.9)

输出结果:
请输入美元:8
55.2
请输入美元:5
34.5
请输入美元:1
6.9

练习1.在控制台输出012345

count = 0
while count < 6:
    print(count)
    count += 1

输出结果
0
1
2
3
4
5

练习二: 在控制太输出234567

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

输出结果:
2
3
4
5
6
7

练习三:在控制台输出 0 2 4 6

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

输出结果:
0
2
4
6

练习4:在控制台中,获取一个开始值,一个结束值
将中间的数字打印出来
例如:开始值3,结束值为10
打印 456789

begin = int(input("请输入一个开始值;"))
end = int(input("请输入结束值:"))

while begin < end - 1:
    begin += 1
    print(begin)

输出结果:
请输入一个开始值;3
请输入结束值:10
4
5
6
7
8
9

练习5.一张纸的厚度是0.01毫米
请计数对折多少此,超过珠穆朗玛峰9944.43米

thickness = 0.01/1000
count = 0 #计数器
while thickness < 8844.43:
    count += 1
    thickness *= 2
    print(thickness)
print(count)

输出结果
2e-05
4e-05
8e-05
0.00016
0.00032
0.00064
0.00128
0.00256
0.00512
0.01024
0.02048
0.04096
0.08192
0.16384
0.32768
0.65536
1.31072
2.62144
5.24288
10.48576
20.97152
41.94304
83.88608
167.77216
335.54432
671.08864
1342.17728
2684.35456
5368.70912
10737.41824
30
  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南城以南1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值