(自学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)
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 = 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()
一个范围的整数
"""
for item in range ( 1 , 5 , 2 ) :
print ( item)
for item in range ( 1 , 5 ) :
print ( item)
for item in range ( 5 ) :
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的作用
sum_value = 0
for item in range ( 1 , 101 ) :
if item % 3 == 0 :
sum_value += item
print ( sum_value)
sum_value = 0
for item in range ( 1 , 101 ) :
if item % 3 != 0 :
continue
sum_value += item
print ( sum_value)
"""
练习:累加10 -- 60之间,个位不是3/5/8的整数和。
思想1:不是3/5/8 则累加
思想2:是3/5/8 则跳过
"""
total_value = 0
for item in range ( 10 , 61 ) :
unit = item % 10
if unit != 3 and unit != 5 and unit != 8 :
total_value += item
print ( total_value)
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
根据次数重复,例如:纸张对折十次
"""