python小题目练习(十)
题目:根据生日判断星座
需求:实现如下图所示结果

代码展示:
"""
Author:mll
Content:根据生日判断星座
Date:2020-11-23
"""
"""
白羊座:3月21日- 4月20日金牛座:4月21日- 5月21日
双子座:5月22日- 6月21日巨蟹座:6月22日- 7月22日
狮子座:7月23日- 8月23日处女座:8月24日- 9月23日
天秤座:9月24日- 10月23日天蝎座:10月24日- 11月22日
射手座:11月23日- 12月21日魔羯座:12月22日- 1月20日
水瓶座:1月21日- 2月19日双鱼座:2月20日- 3月20日
"""
# 定义一个函数进行判断用户输入的月份跟日期是什么星座
def judge(month, date):
if (month == 1 and 21 <= date <= 31) or (month == 2 and date <= 19):
print(str(month) + '月' + str(date) + '日星座为:水瓶座')
elif (month == 2 and 20 <= date <= 29) or (month == 3 and date <= 20):
print(str(month) + '月' + str(date) + '日星座为:双鱼座')
elif (month == 3 and 21 <= date <= 31) or (month == 4 and date <= 20):
print(str(month) + '月' + str(date) + '日星座为:白羊座')
elif (month == 4 and 21 <= date <= 30) or (month == 5 and date <= 21):
print(str(month) + '月' + str(date) + '日星座为:金牛座')
elif (month == 5 and 22 <= date <= 31) or (month == 6 and date <= 21):
print(str(month) + '月' + str(date) + '日星座为:双子座')
elif (month == 6 and 22 <= date <= 30) or (month == 7 and date <= 22):
print(str(month) + '月' + str(date) + '日星座为:巨蟹座')
elif (month == 7 and 23 <= date <= 31) or (month == 8 and date <= 23):
print(str(month) + '月' + str(date) + '日星座为:狮子座')
elif (month == 8 and 24 <= date <= 31) or (month == 9 and date <= 23):
print(str(month) + '月' + str(date) + '日星座为:处女座')
elif (month == 9 and 24 <= date <= 30) or (month == 10 and date <= 23):
print(str(month) + '月' + str(date) + '日星座为:天秤座')
elif (month == 10 and 24 <= date <= 31) or (month == 11 and date <= 22):
print(str(month) + '月' + str(date) + '日星座为:天蝎座')
elif (month == 11 and 23 <= date <= 30) or (month == 12 and date <= 21):
print(str(month) + '月' + str(date) + '日星座为:射手座')
elif (month == 12 and 22 <= date <= 31) or (month == 1 and date <= 20):
print(str(month) + '月' + str(date) + '日星座为:魔羯座')
else:
print('暂时没有符合您的星座, 请你回火星去~~~')
month_input = int(input('请输入月份(例如:5):')) # 用户输入月份
date_input = int(input('请输入日期:(例如:17):')) # 用户输入日期
judge(month_input, date_input) # 函数调用
运行结果展示:
![]()
总结:本实例需要考虑的是在if语句进行条件判断时,大月跟小月的边界值,写完代码后,作为专业测试人员应该多测些异常情况

浙公网安备 33010602011771号