首页 > 代码库 > 4.pytho while 循环

4.pytho while 循环

1.使用while 循环输出1—10并停止循环:示例如下

kaishi = 1
while True:
  print (kaishi)
  if kaishi == 10:
    break
  kaishi = kaishi +1

2.使用while循环算出1+2+3.....+100的求和算法

sum = 0 

start = 1

while True:

#   print (start)

     sum = sum + start

     if start == 100

    break

  start += 1

print (sum)

3.通过while循环输出123456 8910的例子

start = 1

while True:

  if start ==7:

      start += 1

      continue

     print (start)
     if start ==10:

        break

start += 1

4.通过while循环源源不断的输出:

import time
kaishi = 1
while True:
  print(kaishi)
  kaishi+= 1
  time.sleep(1)

5.连续叠加循环:

import time
kaishi = 1
flang = True
while flang:
  print(kaishi)
  if kaishi == 10:
    flang = False
       kaishi+= 1
       time.sleep(1)
print (‘end‘)

6.持续循环1

import time
#a1 = True
while True:
  print (‘1‘)
  time.sleep(1)
# a1 = False

print (‘end‘)

 

4.pytho while 循环