python控制语句执行流程(while)

循环语句
 Python提供了for循环和while循环(在Python中没有do..while循环)
 
 while语句
  格式:
    while 判断条件:
        执行语句....
    
 -->判断条件可以是任何表达式,任何非零、或非空(null)的值均为true
 
 while案例描述:
  count = 0
  while (count < 9):
     print 'The count is:', count
     count = count + 1
  print '结束!'
 
 
  # continue 和 break 用法
  i = 1
  while i < 10:  
   i += 1
   if i%2 > 0:     # 非双数时跳过输出
     continue
   print i         # 输出双数2、4、6、8、10
 
  i = 1
  while 1:            # 循环条件为1必定成立
     print i         # 输出1~10
     i += 1
     if i > 10:     # 当i大于10时跳出循环
        break
 
  无限循环情况
  var = 1
  while var == 1 :  # 该条件永远为true,循环将无限执行下去
     num = raw_input("Enter a number  :")
     print "You entered: ", num
 
  在 python 中,while … else 在循环条件为 false 时执行 else 语句块
  count = 0
  while count < 5:
     print count, " is  less than 5"
     count = count + 1
  else:
     print count, " is not less than 5"
  
  --->当count大于5后,会执行else下的语句
 
  
 猜大小的游戏
 import random
 s = int(random.uniform(1,10))
 m = int(input('输入整数:'))
 while m != s:
  if m > s:
   print('大了')
   m = int(input('输入整数:'))
  if m < s:
   print('小了')
   m = int(input('输入整数:'))
  if m == s:
   print('OK')
   break;
   
 猜拳小游戏
 import random
 while 1:
  s = int(random.randint(1, 3))
  if s == 1:
   ind = "石头"
  elif s == 2:
   ind = "剪子"
  elif s == 3:
   ind = "布"
  m = raw_input('输入 石头、剪子、布,输入"end"结束游戏:')
  blist = ['石头', "剪子", "布"]
  if (m not in blist) and (m != 'end'):
   print "输入错误,请重新输入!"
  elif (m not in blist) and (m == 'end'):
   print "\n游戏退出中..."
   break
  elif m == ind :
   print "电脑出了: " + ind + ",平局!"
  elif (m == '石头' and ind =='剪子') or (m == '剪子' and ind =='布') or (m == '布' and ind =='石头'):
   print "电脑出了: " + ind +",你赢了!"
  elif (m == '石头' and ind =='布') or (m == '剪子' and ind =='石头') or (m == '布' and ind =='剪子'):
   print "电脑出了: " + ind +",你输了!"
其他:
random.random():
方法用于生成一个0到1的随机浮点数:0<=n<1.0
random.uniform(a,b):
用于生成一个指定范围内的随机浮点数,两格参数中,其中一个是上限,一个是下限
如果a>b,则生成的随机数n,即b<=n<=a;如果a>b,则a<=n<=b。
random.randint(a,b):
用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n:a<=n<=b 
random.randint(20,10) 该语句是错误的,下限必须小于上限
2.循环语句
 Python提供了for循环和while循环(在Python中没有do..while循环)
 
 while语句
  格式:
   while 判断条件:
    执行语句....
    
 -->判断条件可以是任何表达式,任何非零、或非空(null)的值均为true
 
 while案例描述:
  count = 0
  while (count < 9):
   print 'The count is:', count
   count = count + 1
  print '结束!'
 
 
  # continue 和 break 用法
  i = 1
  while i < 10:  
   i += 1
   if i%2 > 0:     # 非双数时跳过输出
    continue
   print i         # 输出双数2、4、6、8、10
 
  i = 1
  while 1:            # 循环条件为1必定成立
   print i         # 输出1~10
   i += 1
   if i > 10:     # 当i大于10时跳出循环
    break
 
  无限循环情况
  var = 1
  while var == 1 :  # 该条件永远为true,循环将无限执行下去
   num = raw_input("Enter a number  :")
   print "You entered: ", num
 
  在 python 中,while … else 在循环条件为 false 时执行 else 语句块
  count = 0
  while count < 5:
   print count, " is  less than 5"
   count = count + 1
  else:
   print count, " is not less than 5"
  
  --->当count大于5后,会执行else下的语句
 
  
 猜大小的游戏
 import random
 s = int(random.uniform(1,10))
 m = int(input('输入整数:'))
 while m != s:
  if m > s:
   print('大了')
   m = int(input('输入整数:'))
  if m < s:
   print('小了')
   m = int(input('输入整数:'))
  if m == s:
   print('OK')
   break;
   
 猜拳小游戏
 import random
 while 1:
  s = int(random.randint(1, 3))
  if s == 1:
   ind = "石头"
  elif s == 2:
   ind = "剪子"
  elif s == 3:
   ind = "布"
  m = raw_input('输入 石头、剪子、布,输入"end"结束游戏:')
  blist = ['石头', "剪子", "布"]
  if (m not in blist) and (m != 'end'):
   print "输入错误,请重新输入!"
  elif (m not in blist) and (m == 'end'):
   print "\n游戏退出中..."
   break
  elif m == ind :
   print "电脑出了: " + ind + ",平局!"
  elif (m == '石头' and ind =='剪子') or (m == '剪子' and ind =='布') or (m == '布' and ind =='石头'):
   print "电脑出了: " + ind +",你赢了!"
  elif (m == '石头' and ind =='布') or (m == '剪子' and ind =='石头') or (m == '布' and ind =='剪子'):
   print "电脑出了: " + ind +",你输了!"
其他:
random.random():
方法用于生成一个0到1的随机浮点数:0<=n<1.0
random.uniform(a,b):
用于生成一个指定范围内的随机浮点数,两格参数中,其中一个是上限,一个是下限
如果a>b,则生成的随机数n,即b<=n<=a;如果a>b,则a<=n<=b。
random.randint(a,b):
用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n:a<=n<=b 
random.randint(20,10) 该语句是错误的,下限必须小于上限
 
 
http://www.runoob.com/python/python-while-loop.html

转载于:https://www.cnblogs.com/luochangwei2019/p/10708693.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值