一、while 循环
while关键字后跟随的是一个循环条件判断,解释器首先判断while循环条件是否成立,若成立,则执行代码块,执行完毕后再次判断循环条件是否成立,若成立,则再次执行代码块,知道循环条件不成立,退出循环。
(一)while语句执行过程
设置初始条件(计数器)
while 循环条件(判断 计数器 是否达到 目标次数):
条件满足,做的事情1
条件满足,做的事情2
......
根据题意处理条件(计数器+1)
循环条件不满足时,做的事情
eg:计算100以内的奇数和
sum = 0
n = 1 #理解为计数器
while n < 100: #判断 n(计数器)是否 小于100(目标次数)
sum += n #满足循环条件,进行求和
n += 2 #根据题意处理条件(计数器+2)
print(sum) #循环条件不成立时,输出sum
PS:while也要严格缩进格式,与选择结构一样缩进表示层级关系
while语句中,break用于跳出当层循环,通常与if语句连用;continue是忽略当次迭代的后续代码,直接进入循环的下次迭代。
break示例:
#判断一个数是否为素数(质数)
a = int(input("请输入一个数:"))
b = 2
if a == 1:
print("这个数不是素数")
elif a == 2:
print("这个数是素数")
else:
while a > b:
if a % b == 0:
print("这个数不是素数")
break
b += 1
else:
print("这个数是素数")
continue示例:
num = 1
while num < 10:
num += 1
if num % 2 > 0: # 非双数时跳过输出
continue
print("1~10内的双数为",num) # 输出双数2、4、6、8、10
(二)while嵌套循环(双重循环)
经典案例 1:打印直角三角形
layer = int(input("请输入要打印的行数:"))
index = 1
while index <= layer:
j = 1 #每行*的个数
while j <= index:
print("*",end="")
j += 1
print()
index += 1
经典案例 2:打印九九乘法表
i = 1
while i <= 9:
j = 1
while j<=i:
res = i * j
if res < 10:
print("%s x %s = %s "%(i,j,(i*j)),end=" ")
else:
print("%s x %s = %s "%(i,j,(i*j)),end=" ")
j += 1
print()
i += 1
二、for 循环
(一)for循环遍历
1、for循环可以遍历任意可迭代对象(序列、集合以及数值列表)。
2、break用于跳出当层循环,通常与if语句连用;continue是忽略当次迭代的后续代码,直接进入循环的下次迭代。
(二)for in 语句执行过程
for 循环变量 in 遍历数据集:
语句块(针对列表元素进行操作) #注意缩进
1、range()函数 —— 前闭后开
(1)range(x) —— 生成一个
区间内的“整数列表”。(2)range(x,y) —— 生成一个
区间内的“整数列表”。 (3)range(x,y,step) —— 生成一个
区间内的“整数列表”,数值的变化随step的值变化而变化,step的值默认为1。
2、for嵌套循环
经典案例 :打印菱形
#第一种
layer = int(input("请输入你要打印的行数:"))
#上半部分
for i in range(1,layer//2+2): #前闭后开
#计算空格的个数
space_num = layer - i
for j in range(0,space_num):
print(" ",end="")
#计算*个数
star_num = 2*i-1
for j in range(0,star_num):
print("*",end="")
print("")
#下半部分
for i in range(layer//2,0,-1):
#计算空格的个数
space_num = layer - i
for j in range(0,space_num):
print(" ",end="")
#计算*个数
star_num = 2*i-1
for j in range(0,star_num):
print("*",end="")
print("")
#第二种
rows = int(input("请输入要打印的行数:"))
for i in range(1,rows):#变量i控制行数
#计算空格
for j in range(rows - i):#3~1
print(" ",end="")
#计算“*”个数
for j in range(2 * i - 1):#1~7
if j == 1 or j == 2 * i - 1:
print("*",end="")
else:
print(" ")
print ("")
#菱形的下半部分
for i in range(rows):
for j in range(i):
print(" ",end="")
for j in range(2 * (rows - i) - 1):#5~1
if j == 1 or j == 2 * (rows - i) - 1:
print("*",end="")
else:
print(" ")
print ("")
三、对应练习
三、循环语句(37~54)
Demo37
import random
a = int(random.uniform(0,100))
print("Guess a magic number between 0 and 100")
b = int(input("Enter your guess:"))
while b < a:
print("Your guess is too low")
b = int(input("Enter your guess:"))
while b > a:
print("Your guess is too height")
b = int(input("Enter your guess:"))
if b == a:
print("Yes, the number is %s" %b)
Demo38
n1,n2 = eval(input("请输入两个整数:"))
if n1 > n2:
gcd = "1"
k = 1
while k <= n2:
if n1 % k == 0 and n2 % k == 0:
gcd = k
k += 1
print(gcd)
elif n1 == n2:
print(n1)
else:
gcd = "1"
k = 1
while k <= n1:
if n1 % k == 0 and n2 % k == 0:
gcd = k
k += 1
print(gcd)
Demo39
import random
num = 0
total = 1000000
for i in range(0,total):
x = random.random() * 2 - 1 #random()---产生0~1之间的数
y = random.random() * 2 - 1
distance = (x ** 2 + y ** 2) ** 0.5
if distance <= 1:
num += 1
pi = num / total * 4
print(pi)
Demo40
positives = 0
negatives = 0
sum = 0
while True:
a = int(input("Enter an integer, the input ends if it is 0:"))
if a == 0:
break;
elif a < 0:
negatives += 1
else:
positives += 1
sum += a
total = negatives + positives
average = sum / total
if total1 == 0:
print("You didn't enter any number")
else:
print("The number of positives is %s" %positives)
print("The number of negatives is %s" %negatives)
print("The total is %s" %sum)
print("The average is %.2f" %average)
Demo41
a = int(input("输入一个整数:"))
i = 2
if a == 1:
print(1)
elif a == 2:
print(1,2)
else:
while i < a:
if a % i == 0:
print(i ,end= ",")
a //= i
else:
i += 1
print(int(a))
Demo42
a = int(input("请输入你要打印的行数:"))
for i in range(1,a + 1):
for j in range(a,0,-1):
if j <= i:
print("%s " %j,end="")
else:
print(" ",end="")
for j in range(2,a+1):
if j<=i:
print('%s ' %j,end="")
print("")
Demo43
#a
print("模式A")
for i in range(1,7):
for j in range(1,7):
if j<=i:
print('%s ' %j,end="")
print("")
#b
print("模式B")
for i in range(6,0,-1):
for j in range(1,7):
if j <= i:
print("%s " %j,end="")
print("")
#c
print("模式C")
for i in range(1,7):
for j in range(6,0,-1):
if j <= i:
print("%s " %j,end="")
else:
print(" ",end="")
print("")
#d
print("模式D")
for i in range(6,0,-1):
a = 6 - i
print(" " *a,end="")
for j in range(1,7):
if j <= i:
print("%s " %j,end="")
print("")
Demo44
a = int(input("请输入一个数:"))
for i in range(0,a):
for j in range(a - i,0,-1):
print("",end="\t")
for k in range(1,i + 1):
print(2 ** (k - 1),end="\t")
for j in range(i + 1,0,-1):
print(2 ** (j - 1),end="\t")
print("")
Demo45
count = 0
for num in range(2,1001):
for j in range(2,num):
if (num % j == 0):
break;
else:
print(num,end="\t")
count += 1
if count % 8 == 0:
print("")
Demo46
sum = 0
for j in range(10000,100001,10000):
sum += 0
for i in range(1,j+1):
sum += ((-1) ** i + 1 ) / (2*i-1)
PI = 4 * sum
print("当i为%d时,PI为%.9f" %(j,PI))
Demo47
for j in range(10000,100001,10000):
e = 1
item = 1
for i in range(1,j + 1):
item *= i #item的阶乘 item = item *
e += 1 / item
print("i = %d 时, e = %.19f"%(j,e))
Demo48
for i in range(1,10001):
sum = 0
for j in range(1,i):
if i % j == 0:
sum += j
if i == sum:
print(i)
Demo49
import random
user_count = 0
com_count = 0
while True:
user= int(random.uniform(0,2))
com = int(input("scissor (0),rock (1),paper(2):"))
win = user - com
if win == -2 or win == 1:
user_count += 1
if user_count == 2:
print("恭喜,你赢了!!!")
break
elif win == 2 or win == -1:
com_count += 1
if com_count == 2:
print("很遗憾,电脑赢了!!!")
break
Demo50
a = int(input("Enter a number(0: for end of input) :"))
max = 0
count = 0
total = 0
while a != 0:
if a >= max:
max = a
count += 1
else:
count -= 1
a = int(input("Enter a number(0: for end of input) :"))
print("The largest number is %s"%max)
print("The occurrence of the largest number is %s"%count)
Demo51
num = int(input("请输入一个数:"))
a = ""
while num != 0:
b = num % 2
a = str(b) + a
num = num // 2
print(a)
Demo52
a = int(input("请输入一个十进制整数:")) #bin二进制,oct八进制,hex十六进制
print("十六进制为{:x}".format(a,a,a,a))
Demo53
import random
total = 1000000
count = 0
for i in range(0,total):
x = random.random() * 2 - 1
y = random.random() * 2 - 1
if -1 <= x <= 0 and -1 <= y <= 1:
count += 1
elif 0 <= x <= 1 and 0 <= y <= 1 and y / (1 - x) <= 1:
count += 1
print("1000000个随机点落在奇数区域的概率是%.12f" %(count / total))
Demo54
count = 0
for year in range(2001,2101):
if year % 4 == 0 and year % 100!=0 or y % 400 ==0:
print(year,end = " ")
count += 1
if count % 10 == 0:
print()