Python校内实训--第二天星座查询进阶版
使用函数调用的方式实现星座查询,也使用了终端操作:
star.py
"""STAR
Usage:
star adjust <year> <month> <day>
Options:
--year 年份
--month 月份
--day 日期
"""
from docopt import docopt
from star_farl import *
if __name__ == '__main__':
arguments=docopt(__doc__)
#获取用户传入的年月日
year=int(arguments['<year>'])
month=int(arguments['<month>'])
day=int(arguments['<day>'])
if adjust_date(year,month,day):
adjust_star_ftal(month,day)
else:
print('请确定日期格式输入正确')
star_farl.py
#判断指定的年份是否是闰年
def adjust_rn(year):
if year%4==0 and year%100 !=0 or year%400==0:
return True
else:
return False
#判断年月日是否合法
def adjust_date(year,month,day):
month1 = [1,3,5,7,8,10,12]
month2 = [4,6,9,11]
if month in month1:
if day >0 and day <=31:
return True
else:
return False
elif month in month2:
if day >0 and day <=30:
return True
else:
return False
elif month ==2:
if adjust_rn(year) ==True:
if day > 0 and day <= 29:
return True
else:
return False
else:
if day > 0 and day <= 28:
return True
else:
return False
else:
return False
#定义函数完成星座的判断
def adjust_star_ftal(month,day):
if month == 1:
if day <= 19:
print('你是摩羯座的,诞生石:土耳其玉')
else:
print('你是水瓶座的,诞生石:紫水晶')
elif month == 2:
if day <= 18:
print('你是水瓶座的,诞生石:紫水晶')
else:
print('你是双鱼座的,诞生石:月长石')
elif month == 3:
if day <= 20:
print('你是双鱼座的,诞生石:月长石')
else:
print('你是白羊座的,诞生石:钻石')
elif month == 4:
if day <= 19:
print('你是白羊座的,诞生石:钻石')
else:
print('你是金牛座的,诞生石:蓝宝石')
elif month == 5:
if day <= 20:
print('你是金牛座的,诞生石:蓝宝石')
else:
print('你是双子座的,诞生石:玛瑙')
elif month == 6:
if day <= 21:
print('你是双子座的,诞生石:玛瑙')
else:
print('你是巨蟹座的,诞生石:珍珠')
elif month == 7:
if day <= 22:
print('你是巨蟹座的,诞生石:珍珠')
else:
print('你是狮子座的,诞生石:红宝石')
elif month == 8:
if day <= 22:
print('你是狮子座的,诞生石:红宝石')
else:
print('你是处女座的,诞生石:红条纹玛瑙')
elif month == 9:
if day <= 22:
print('你是处女座的,诞生石:红条纹玛瑙')
else:
print('你是天枰座的,诞生石:蓝宝石')
elif month == 10:
if day <= 23:
print('你是天枰座的,诞生石:蓝宝石')
else:
print('你是天蝎座的,诞生石:猫眼石')
elif month == 11:
if day <= 22:
print('你是天蝎座的,诞生石:猫眼石')
else:
print('你是射手座的,诞生石:黄宝石')
elif month == 12:
if day <= 21:
print('你是射手座的,诞生石:黄宝石')
else:
star.py
"""STAR
Usage:
star adjust <year> <month> <day>
Options:
--year 年份
--month 月份
--day 日期
"""
from docopt import docopt
from star_farl import *
if __name__ == '__main__':
arguments=docopt(__doc__)
#获取用户传入的年月日
year=int(arguments['<year>'])
month=int(arguments['<month>'])
day=int(arguments['<day>'])
if adjust_date(year,month,day):
adjust_star_ftal(month,day)
else:
print('请确定日期格式输入正确')
star_farl.py
#判断指定的年份是否是闰年
def adjust_rn(year):
if year%4==0 and year%100 !=0 or year%400==0:
return True
else:
return False
#判断年月日是否合法
def adjust_date(year,month,day):
month1 = [1,3,5,7,8,10,12]
month2 = [4,6,9,11]
if month in month1:
if day >0 and day <=31:
return True
else:
return False
elif month in month2:
if day >0 and day <=30:
return True
else:
return False
elif month ==2:
if adjust_rn(year) ==True:
if day > 0 and day <= 29:
return True
else:
return False
else:
if day > 0 and day <= 28:
return True
else:
return False
else:
return False
#定义函数完成星座的判断
def adjust_star_ftal(month,day):
if month == 1:
if day <= 19:
print('你是摩羯座的,诞生石:土耳其玉')
else:
print('你是水瓶座的,诞生石:紫水晶')
elif month == 2:
if day <= 18:
print('你是水瓶座的,诞生石:紫水晶')
else:
print('你是双鱼座的,诞生石:月长石')
elif month == 3:
if day <= 20:
print('你是双鱼座的,诞生石:月长石')
else:
print('你是白羊座的,诞生石:钻石')
elif month == 4:
if day <= 19:
print('你是白羊座的,诞生石:钻石')
else:
print('你是金牛座的,诞生石:蓝宝石')
elif month == 5:
if day <= 20:
print('你是金牛座的,诞生石:蓝宝石')
else:
print('你是双子座的,诞生石:玛瑙')
elif month == 6:
if day <= 21:
print('你是双子座的,诞生石:玛瑙')
else:
print('你是巨蟹座的,诞生石:珍珠')
elif month == 7:
if day <= 22:
print('你是巨蟹座的,诞生石:珍珠')
else:
print('你是狮子座的,诞生石:红宝石')
elif month == 8:
if day <= 22:
print('你是狮子座的,诞生石:红宝石')
else:
print('你是处女座的,诞生石:红条纹玛瑙')
elif month == 9:
if day <= 22:
print('你是处女座的,诞生石:红条纹玛瑙')
else:
print('你是天枰座的,诞生石:蓝宝石')
elif month == 10:
if day <= 23:
print('你是天枰座的,诞生石:蓝宝石')
else:
print('你是天蝎座的,诞生石:猫眼石')
elif month == 11:
if day <= 22:
print('你是天蝎座的,诞生石:猫眼石')
else:
print('你是射手座的,诞生石:黄宝石')
elif month == 12:
if day <= 21:
print('你是射手座的,诞生石:黄宝石')
else:
print('你是摩羯座的,诞生石:土耳其玉')
运行结果截图: