#!user/bin/python
#encoding:utf8
#while
time = 0
population = 1000 # 1000 bacteria to start with
grow_rate = 0.21 # 21% growth per minute
while population < 2000:
population = population + grow_rate * population
print population
time = time + 1
print "It took %d minutes for the bacteria to double." % time
print "...and the final population was %6.2f bacteria." % population
text = ' '
while text != "quit":
text = raw_input("Please enter a chemical formula (or 'quit' to exit): ")
if text == "quit":
print "...exiting program."
elif text == "H2O":
print "Water"
elif text == "NH3":
print "Ammonia"
elif text == "CH3":
print "Methane"
else:
print "Unknown compound"
#break
earth_line = 1
file = open("data.txt","r")
for line in file:
line = line.strip()
if line == "Earth":
break
earth_line += 1
print "Earth is at the line %d" % earth_line
file.close()
#continue
entry_number = 1
file = open("data.txt","r")
for line in file:
line = line.strip()
if line.startswith('#'):
continue
if line == "Earth":
break
entry_number += 1
print "Earth is the %dth-lightest planet." % entry_number
<<Python编程实践>>之WhileBreakContinue
原创Digital2Slave 博主文章分类:Python ©著作权
©著作权归作者所有:来自51CTO博客作者Digital2Slave的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Python数据库编程全指南SQLite和MySQL实践
本文介绍了使用Python进行数据库连接与操作的多种方法和技术。
SQL mysql Python SQLite -
linux之socket编程
linux之socket编程
数据 端口号 IP -
敏捷实践之结对编程Pair
本文是
Pair 解决方案 编写代码 -
Storm之——流组件简单串行编程实践
Storm是一个分布式是实时计算系统,它设
storm ide java apache