您的位置:首页 > 编程语言 > Python开发

python中while

2019-04-04 11:46 176 查看

01.

“”"
while 条件():
条件1满足时,做的事情1
条件2满足时,做的事情2
“”"

#1.定义一个变量,记录循环次数
i = 1

#2.开始循环
while i <= 3:			##设定循环三次
#循环内执行的动作
print('hello python')
#处理计数器
i += 1

02.while的死循环

while True:
print('hello python')

03.while求和

求0到100所有整数的和

i = 0

result = 0

while i <= 100:
result += i
i += 1

print('和为:%d' %result)

04.while的嵌套

用whille的嵌套生成:

*****
****
***
**
*
row = 5
while row >= 1:
col = 1
while col <= row:
print('*',end='')
col += 1
print('')
row -= 1

用while写出九九乘法表

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

猜数字游戏,五次机会

import random

trycount = 0
computer = random.randint(1,100)
while trycount < 5:
player = int(input('Num:'))
if player > computer:
print('too big')
trycount += 1
elif player < computer:
print('too small')
trycount += 1
else:
print('恭喜')
break
print(computer)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: