Python 循环(while)

2018-07-13  本文已影响0人  白s圣诞节

一、while

while循环

i = 1
sum = 0
while i <= 100:
     print(i)
     sum+=i #sum = sum + i
      i+=1
 print(sum)

二、死循环:

*概括:

不断进行,不会停止。

while True:
print('我会重复的')
   while True:
       import random
       player = int(input("请出拳 1石头/2剪刀/3布"))
      random.randint(1,3)
       pc = random.randint(1,3)
       if (player == 1 and pc == 2) or (player == 2 and pc ==3  or (player ==     3 and pc == 1):
          print("玩家赢")
       elif player == pc:
           print("平局")
      else:
          print("电脑赢")

三、循环中断:break

*概括:

breaak 某一条件满足时,退出循环,不在执行后续重复的代码。

*例如:

  i = 1
   while i < 101:
       print(i)
       i+=1
       if i == 50:
           break
   print(i)

四、循环中断:continue

*概括:

continue 某一条件满足时,不执行后续重复的代码。

*例如:

i = 0
while i < 11:
  i +=1
  if i == 6
      print("中断")
      continue
上一篇 下一篇

猜你喜欢

热点阅读