11-while循环基本使用

时间:2024-08-28 21:35:56

hm_02_第一个while循环.py

 def main():
i = 1
while i <= 3:
print(i, 'Hello world')
i += 1
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')