while循环
循环结构
while 条件:
print("any")
循环体
条件为假时,结束循环
#输出1~100之间所有偶数
num = 0
while num <= 100:
if num%2 == 0:
print(num)
num += 1
#输出1~100之间的奇数同上
#猜年龄,直到用户输入正确的年龄
age_of_principal = 56
flag = True
while flag:
guess_age = int (input(">>:"))
if guess_age == age_of_principal:
print("yes")
flag = False
elif guess_age > age_of_principal:
print("be smaller")
else:
print("be bigger")
print("end!")
while...break
break:终止循环
#猜年龄,直到用户输入正确的年龄
age_of_principal = 56
flag = True
while flag:
guess_age = int (input(">>:"))
if guess_age == age_of_principal:
print("yes")
break #flag = False
elif guess_age > age_of_principal:
print("be smaller")
else:
print("be bigger")
print("end!")
#while循环break、continue
#break:终止循环
#continue:跳出当前循环
num = 1
while num <= 10:
num += 1
#print(num)
if num == 3:
continue
print(num)
#while循环break、continue
#break:终止循环
#continue:跳出当前循环
'''num = 1
while num <= 10:
num += 1
#print(num)
if num == 3:
continue
print(num)'''
'''while条件
...
else
...'''
num = 1
while num <= 10:
num += 1
if num == 3:
continue
print(num)
else:
print("this is else statement")
num = 1
while num <= 10:
num += 1
if num == 3:
break
print(num)
else:
print("this is else statement")
九九乘法表
#九九乘法表:multiplication_table
'''
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
。。。。。。
思路
先输出一个三角形
*
**
***
****
*****
******
*******
********
*********
第一次:输出1个*加换行
第二次:输出2个*加换行
第三次:输出3个*加换行
即:外循环第一次循环,内循环循环1次
外循环第二次循环,内循环循环2次
外循环第三次循环,内循环循环3次
。。。。。。
1、外循环循环9次
2、内循环次数取决于第几次外循环
提取变量
第几次:counts = 1、2、3、4...9
输出星:star_numbers = 1、2、3、4...9
两个循环
counts = 0
star_numbers = 0
while counts <= 9:
counts += 1
print("*",end"\n")
'''
a = 1
while a <= 9:
#a += 1
b = 1
while b <= a:
#print("*",end="")
print(a,"*",b,"=",a*b,end=" ")
b += 1
a += 1
print()
#print(a,"*",b,"=",a*b)