美文网首页
Python 循环(while)

Python 循环(while)

作者: 白s圣诞节 | 来源:发表于2018-07-13 17:26 被阅读0次

一、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

相关文章

网友评论

      本文标题:Python 循环(while)

      本文链接:https://www.haomeiwen.com/subject/fmhspftx.html