python循环结构方法_Python 基础之循环结构 while

一.while循环介绍

while 循环 可以提高代码的效率,减少代码的冗余

while 条件表达式:code1code2如果条件表达式成立,返回Ture,就执行其中的代码块

1.基本语法

例:打印1 ~100

#(1)初始化变量i

i = 1

#(2)写循环判断条件

while i<= 100:

print(i)

#(3)自增自减的条件

i+=1

#代码解析

首先初始化i=1

然后判断1<=100 满足返回Ture执行代码块

然后print(1)

i+=1 i = i + 1 i =>2

回到条件表达式当中进行判断 也就是回答20行

2<=100 满足 返回Ture执行代码块

i+=1 i= i+1 2+i =>3

回到条件表达式当中进行判断 ,也就是回到20行

3<=100 满足 返回Ture执行代码块

然后print(3)

i+=1 i = i + 1 3+1 =>4

什么时候条件跳出?

当i=101的时候

101<=100 不满足 返回False不执行代码块

循环终止。。。

2.while的写法

例:

#(1)第一种方法

#1~100的累加和

i = 1

total = 0

while i <= 100:

#写上逻辑

#print(i)

total += 1

i += 1

print(total)

代码解析:

total += i => total = total + i => 0 + 1 => 1

i+=1 => i = i+1 => 1+1 => 2

2 <= 100  满 足 返回真True

total += i => totoal = total + i => 0 + 1 + 2 => 3

i+=1 => i = i+1 => 2+1 => 3

3 <= 100  满足  返回真True

total += i => totoal = total + i => 0 + 1 + 2 + 3 => 6

i+=1 => i = i+1 => 3+1 => 4

4 <= 100 满足 返回真True

total += i => totoal = total + i => 0 + 1 + 2 + 3 + 4 + 5 + ...+ 100 => 5050

dai

当i = 101 的时候

101 <= 100 不满足 循环终止...

# (2)死循环写法

#whilie Ture:

#print(1)

i = 1

flag = True

total = 0

while flag:

total += i

i +=1

# 添加一个能够跳出来的条件   if i == 101:

flag = False

print(total)

#打印一行10个小星星

#help 可以查看帮助文档

help(print)

i = 1

while i <=100:

#end = '' 默认不换行

print("*",end="")

i+=1

#用一个变量输出10个小星星(配置while)

i = 0

whilei < 10:

print("★",end="")

i+=1

二.关于while循环的练习题

1.用一个变量打印出一个十个小星星

(十个小星星塞在一个变量中,最后达成变量)例:

#法一:

a = 10

res = "★" * a

print(res)

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

#法二:

i = 0

strvar = ''

while i<10:

strvar += "★"

i+=1

print(strvar)

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

#法三:

i = 0

while i < 10:

print("★",end="")

i+=1

print("\n",end="===============\n")

#法四:

print("★★★★★★★★★★")

2.十行十个小星星

例:

#法一:

i = 0

while i < 100:

#打印星星

print("☆",end="")

#打印换行

if i%10 == 9:

print()

i+=1

#法二:

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

i = 0

while i <= 10:

j = 0

while j <= 10:

print("☆",end="")

j += 1

print("\n",end="")

i += 1

#法三:

#输入数量几就输出几个几行小星星,且奇数为★,偶数为☆

i = 0

n = float(input("请输入星星个数:"))

while i < n :

if n % 2  == 0:

j = 1

while j <= n:

print("☆", end="")

j += 1

print("\n", end="")

i+=1

else:

j = 1

while j <= n:

print("★", end="")

j += 1

print("\n", end="")

i += 1

#法四:

j = 0

n = int(input("请输入星星个数:"))

for j in range(0,n):

i = 0

strvar = ''

while i

if n % 2 == 0:

strvar += "★"

else:

strvar += "☆"

i+=1

print(strvar)

j+=1

3.十行十列隔列变色小星星

例:

#法一

i = 1

while i <= 100:

#控制打印 隔列打印

if i % 2==0:

print("★", end="")

else:

print("☆",end="")

#控制换行

if i % 10 == 0:

print()

i +=1

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

#法二:

i = 0

strvar = ''

j = 0

while j < 10:

while i < 10:

if i%2 == 0:

strvar +="★"

else:

strvar += "☆"

i+=1

print(strvar)

j += 1

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

#法三:

j = 0

#对行的星星进行排序

for j in range(0,10):

i = 0

strvar = ''

#对列的星星进行排列

while i < 10:

if i%2 == 0:

strvar +="★"

else:

strvar += "☆"

i+=1

print(strvar)

j+=1

#法四

count = 1

while count <= 100:

if count & 1:

print("★", end="")

else:

print("☆",end="")

if count % 10 == 0:

print("\n",end="")

count +=1

print("=======最后一种========")

i = 0

while i < 10:

j = 0

while j < 10:

#控制打印星星的代码

if j % 2 == 0:

print("★",end="")

else:

print("☆",end="")

j+=1

print()

i += 1

4.十行十列隔行变色小星星

例:

#法一

i = 0

while i < 10:

j = 0

if i%2 == 0:

while j <= 10:

print("★",end="")

j += 1

print("\n",end="")

else:

while j <= 10:

print("☆",end="")

j += 1

print("\n",end="")

i += 1

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

解析:

n // 2

0 // 2 0

1 // 2 0

2 // 2 1

3 // 2 1

0 // 4 0

1 // 4 0

2 // 4 0

3 // 4 0

4 // 4 1

..

..

4个1 4个2 4个3

任意数和n进行地板除  产生n个相同的数

任意数 // 10 产生十个相同的数

任意数 // 25 产生25个相同的数

#法二

i = 0

while i< 100:

#打印星星的部分

if i // 10 %2 == 0:

print("★",end="")

else:

print("☆",end="")

#执行部分

if i % 10 == 9:

print()

i+=1

解析:

i = 0 1 2 3 4 5 6 7 8 9

i // 10 0

i = 10 11 12 13 14 15 16 17 18 19

i //10 1

...........

i // 10 2

i // 10 得到最后的数范围是0~9

0~9 % 2  0 或者1正好做黑白星

5.输入几行几列就输出几行几列隔列变色小星星

例:

j = 0

m = int(input("请输入小星星的行数:"))

n = int(input("请输入小星星的列数:"))

#对行的星星进行排序

for j in range(0,m):

i = 0

strvar = ''

#对列的星星进行排列

while i < n:

if i%2 == 0:

strvar +="★"

else:

strvar += "☆"

i+=1

print(strvar)

j+=1

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值