11-while循环基本使用

hm_02_第一个while循环.py

1 def main():
2     i = 1
3     while i <= 3:
4         print(i, 'Hello world')
5         i += 1
6     print(i)

1 Hello world
2 Hello world
3 Hello world
4

hm_03_程序计数.py

def main():
    i = 0
    while i < 3:
        print(i, 'Hello world')
        i += 1
    print(i)

hm_04_累加求和.py

def main():
    sum = 0
    i = 0
    while i <= 100:
        sum += i
        i += 1
    print(i, sum)

101 5050

 

hm_05_偶数求和.py

def main():
    sum = 0
    i = 0
    while i <= 100:
        if i % 2 == 0:
            print(i)
            sum += i
        i += 1
    print(i, sum)

101 2550

 

hm_06_break.py

def main():
    i = 0
    while i < 10:
        if i == 3:
            break
        print(i)
        i += 1

0
1
2

hm_07_continue.py

def main():
    i = 0
    while i < 10:
        if i == 3:
            i += 1
            continue
        print(i)
        i += 1

0
1
2
4
5
6
7
8
9

 

hm_08_打印小星星.py

def main():
    row = 0
    while row <= 5:
        print('*' * row)
        row += 1

*
**
***
****
*****

 

hm_09_print函数的结尾.py

def main():
    print('*' * 10, end='---')

**********---

 

hm_10_嵌套打印小星星.py

def main():
    row = 0
    while row < 5:
        col = 0
        while col <= row:
            print('*', end='')
            col += 1
        print('')
        row += 1

 

hm_11_九九乘法表.py

def main():
    row = 1
    while row <= 9:
        col = 1
        while col <= row:
            print('%d*%d=%d' % (col, row, col * row), end='\t')
            col += 1
        print('')
        row += 1

 

hm_12_转义字符.py

def main():
    print('1\t2')
    print('hello\nworld')

 

转载于:https://www.cnblogs.com/hbzxpost/p/10441297.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值