<<Python编程实践>>之WhileBreakContinue

来源:互联网 发布:数据库管理系统教程 编辑:程序博客网 时间:2024/06/14 09:53
#!user/bin/python#encoding:utf8#whiletime = 0population = 1000 # 1000 bacteria to start withgrow_rate  = 0.21 # 21% growth per minutewhile population < 2000:population = population + grow_rate * populationprint populationtime = time + 1print "It took %d minutes for the bacteria to double." % timeprint "...and the final population was %6.2f bacteria." % populationtext = ' '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"#breakearth_line = 1file = open("data.txt","r")for line in file:line = line.strip()if line == "Earth":breakearth_line += 1print "Earth is at the line %d" % earth_linefile.close()#continueentry_number = 1file = open("data.txt","r")for line in file:line = line.strip()if line.startswith('#'):continueif line == "Earth":breakentry_number += 1print "Earth is the %dth-lightest planet." % entry_number

0 0