python中的while语句
来源:互联网 发布:南京魔苹网络能去吗 编辑:程序博客网 时间:2024/06/06 02:48
循环使用 else 语句
在 python 中,while … else 在循环条件为 false 时执行 else 语句块:
实例
#!/usr/bin/python
count = 0
whilecount <5:
printcount," is less than 5"
count =count +1
else:
printcount," is not less than 5"
以上实例输出结果为:
0 is less than 51 is less than 52 is less than 53 is less than 54 is less than 55 is not less than 5numbers=[1,2,3,4,5]
even=[ ]
odd=[ ]
while len(numbers)>0:
number=numbers.pop()
if(number%2==0):
even.append(number)
else:
odd.append(number)
猜大小的游戏
#!/usr/bin/python# -*- coding: UTF-8 -*-import randoms = int(random.uniform(1,10))#print(s)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;
猜拳小游戏
#!/usr/bin/python# -*- coding: UTF-8 -*-import randomwhile 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 +",你输了!"
测试结果:
输入 石头、剪子、布,输入"end"结束游戏:石头电脑出了: 石头,平局!输入 石头、剪子、布,输入"end"结束游戏:石头 电脑出了: 剪子,你赢了!输入 石头、剪子、布,输入"end"结束游戏:
摇筛子游戏
#!/usr/bin/env python3# -*- coding: utf-8 -*-import randomimport sysimport timeresult = []while True: result.append(int(random.uniform(1,7))) result.append(int(random.uniform(1,7))) result.append(int(random.uniform(1,7))) print result count = 0 index = 2 pointStr = "" while index >= 0: currPoint = result[index] count += currPoint index -= 1 pointStr += " " pointStr += str(currPoint) if count <= 11: sys.stdout.write(pointStr + " -> " + "小" + "\n") time.sleep( 1 ) # 睡眠一秒 else: sys.stdout.write(pointStr + " -> " + "大" + "\n") time.sleep( 1 ) # 睡眠一秒 result = []
十进制转二进制
#!/usr/bin/python# -*- coding: UTF-8 -*-denum = input("输入十进制数:")print denum,"(10)",binnum = []# 二进制数while denum > 0: binnum.append(str(denum % 2)) # 栈压入 denum //= 2print '= ',while len(binnum)>0: import sys sys.stdout.write(binnum.pop()) # 无空格输出print ' (2)'
while循环 - 九九乘法表
#!/usr/bin/python# -*- coding: UTF-8 -*-#九九乘法表i = 1while i : j = 1 while j: print j ,"*", i ," = " , i * j , ' ', if i == j : break j += 1 if j >= 10: break print "\n" i += 1 if i >= 10: break这是一些博客主提供的非常好的例子值得借鉴学习。
阅读全文
0 0
- python中的while语句
- python 学习while语句中的break&continue
- 2.9 python中的while循环语句 [python入门教程]
- python的while语句
- Python While循环语句
- 3.2 Python while语句
- python while()语句
- Python--While循环语句
- Python While 循环语句
- Python While 循环语句
- Python While 循环语句
- Python While 循环语句
- Python While 循环语句
- python中的while和for语句的简单使用
- python中的while循环
- python中的 while True
- python学习之while语句
- Python循环语句(while)
- jdk源码解析(一)
- [LCT] BZOJ2759.一个动态树好题
- leetcode: 653. Two Sum IV
- Maven 项目管理工具
- Android中的SP、DP等的关系和概念
- python中的while语句
- 共现分析
- 算法练习(10):Merge k Sorted Lists
- 多线程
- js高程-前3章笔记
- DELETE_FAILED_INTERNAL_ERROR Error while Installing APKs 解决:File->Settings->Build,Execuion,Deploymen
- Android studio 导入另外一个项目作为依赖包
- 对象拷贝类PropertyUtils,BeanUtils,BeanCopier的技术沉淀(1)------功能简介
- constraints of a model of DPD