Skip to main content

Python while循环

Published 1 min readMarkdown
Python while循环

while循环

什么是while循环

  • 以一定条件为基础的循环,条件满足则无限循环,条件不满足退出循环
  • 不依赖可迭代的数据类型,而for循环依赖

功能

  • 在满足条件下会无限循环,不满足条件后将停止循环

while用法

while bool_result:
    do

参数

  • bool_result: 布尔类型,此处与If语法完全一致

  • do: while循环体的代码块 #需要缩进

返回值

  • while循环是语句,没有返回值

代码

# coding:utf-8

count = 0
total = 0

while count <= 100:
    total += count
    count += 1

    if count == 10:
        print('count 已经到10了')
    elif count == 50:
        print('count 已经到50了')
    elif count == 99:
        print('count 马上就要到100了!')

print(total)

users = ['dewei', 'xiaomu', 'xiaogang', 'xiaoming']
index = 0
length = len(users)

while index <= length - 1:
    print(users[index])
    index += 1

while True:
    print('xx')

1 view

No comments yet. Be the first to comment.

More from this blog