参考书籍:python程序设计
chapter7
5.
#This pro print the healthy level based on the BMI.
#input:weight(kg), and height(cm).
#output:healthy level
def ConvertWeight(weight):
#This pro convert weight kg to bounds.
return 2.2046 * weight
def ConvertHeight(height):
#This pro convert height cm to inch
return 0.3937 * height
def main():
weight = float(input("Enter your weight(kg):"))
height = float(input("Enter your height(cm):"))
#convert weight and height
weight = ConvertWeight(weight)
height = ConvertHeight(height)
#cal the bmi
BMI = 720 * weight / height ** 2
#healthy level
if BMI < 19:
level = "up"
elif BMI <= 25:
level = "in"
else:
level = "down"
#print
print("your healthy level is {0} the healthy area.".format(level))
main()
11.
#This pro judges whether the year is leap year.
#input: year number
#output: leap year or not
def main():
year = int(input("Enter the year number:"))
if year % 100 == 0:
if year % 400 == 0:
whether = "is"
else:
whether = "is not"
elif year % 4 == 0:
whether = "is"
else:
whether = "is not"
print("year {0} {1} leap year.".format(year, whether))
main()
12.
#This pro vertify the date is valid.
#input:date in format month/day/year
#output:whether valid or not
def WhetherLeap(year):
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
elif year % 4 == 0:
return True
else:
return False
def main():
month, day, year = input("Enter the date like month/day/year:").split('/')
month = int(month)
day = int(day)
year = int(year)
if month in [1, 3, 5, 7, 9, 10, 12]:
if 1 <= day <= 31:
flag = "valid"
else:
flag = "not valid"
elif month in [4, 6, 9, 11]:
if 1 <= day <= 30:
flag = "valid"
else:
flag = "not valid"
elif month == 2:
if WhetherLeap(year):
if 1 <= day <= 29:
flag = "valid"
else:
flag = "not valid"
else:
if 1 <= day <= 28:
flag = "valid"
else:
flag = "not valid"
else:
flag = "not valid"
print("The date is {0}.".format(flag))
main()
13.
#This pro count the days in a year.
#input:date in format month/day/year
#output:days counter
def WhetherLeap(year):
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
elif year % 4 == 0:
return True
def WhetherValid(month, day, year):
if month in [1, 3, 5, 7, 9, 10, 12]:
if 1 <= day <= 31:
return True
else:
return False
elif month in [4, 6, 9, 11]:
if 1 <= day <= 30:
return True
else:
return False
elif month == 2:
if WhetherLeap(year):
if 1 <= day <= 29:
return True
else:
return False
else:
if 1 <= day <= 28:
return True
else:
return False
else:
return False
def main():
month, day, year = input("Enter the date like month/day/year:").split('/')
month = int(month)
day = int(day)
year = int(year)
if WhetherValid(month, day, year):
if month <= 2:
dayNum = 31 * (month - 1) + day
else: #month > 2
if WhetherLeap(year):
dayNum = 31 * (month - 1) + day - (4 * month + 23) // 10 + 1
else: #not a leap year
dayNum = 31 * (month - 1) + day - (4 * month + 23) // 10
print("day num is: ", dayNum)
if __name__ == "__main__":
main()
17.
#This pro move the circle to the click zone 10 times.
from graphics import *
def drawCircle(center, size):
myCircle = Circle(center, size)
myCircle.setOutline('black')
myCircle.setWidth(1)
return myCircle
def drawWin(dx, dy, ux, uy):
win = GraphWin("Moving", 400, 400)
win.setBackground('white')
win.setCoords(dx, dy, ux, uy)
return win
def main():
#draw the win
weight = 100
height = 100
win = drawWin(0, 0, weight, height)
#draw the circle
center = Point(50, 50)
size = 10
circle = drawCircle(center, size)
circle.draw(win)
#move the circle
dx, dy = 1, 1
for i in range(200):
circle.move(dx, dy)
poin = circle.getCenter()
#about x
if poin.getX() + size > weight:
dx = -1
elif poin.getX() - size < 0:
dx = 1
#about y
if poin.getY() + size > height:
dy = -1
elif poin.getY() - size < 0:
dy = 1
#update
update(30) #pause so rate is not more than 30 times a second.
#click to quit
print("Click to quit.")
win.getMouse()
win.close()
if __name__ == "__main__":
main()