while和for的简单使用


1.while 循环 到99乘法表

a=9
while a>0:
    i=1
    while i<=a:
        print('%d * %d = %2d' % (a,i, a*i),end='  ')
        i+=1
    print()
    a-=1

2.使用for循环 做三角形

for i in range(5):
    print("*" * (i+1))

3.倒三角

a=5
for i in range(1,6):
    print("*" * a)
    a-=1

4.使用for循环输出数字0-9,当数字为6时,跳出本次循环,执行其他循环,当数字为8的时候,停止循环

for i in range(0,10):
    if i==6:
        continue
    elif i==8:
        break
    else:
        print(i)

5.使用循环 计算出1-100之间 所有基数的和

i=1
sum1=0
sum2=0
while i<=100:
    if i%2==0:
        sum1+=i
    else:
        sum2+=i
    i+=1
print("1-100之间偶数和为: %d" % sum1)
print("1-100之间奇数和为: %d" % sum2)

原文地址:https://www.cnblogs.com/lxs1030/p/14138280.html