while 1: #while 只是方便重复输入,不用再次重新运行程序 info = str(input('请按照****-**-**的格式输入:')) month = int(info[5:7]) day = int(info[8:10]) # input()提示输入,括号内是提示内容 #info = str(input('请按照****-**-**的格式输入:')) #可用print('请按照****-**-**的格式输入:') #info=input() #info=str(info)代替 #month = int(info[5:7])中使用了字符串的截取,[5:7]取的是字符串中第6到第7个字符。因为下标是从0开始的 #例如 2006-02-26,[5:7]截取的结果为02 #day = int(info[8:10]),同上,[8:10]截取字符串中第9到第10个字符,[8:10]截取的结果为26,下文星座查询可用到 #星座查询:输入生日,显示该日期是什么星座。 # 白羊座 # 3 # 月21日 - ------4 # 月19日 # 诞生石: 钻石 # 金牛座 # 4 # 月20日 - ------5 # 月20日 # 诞生石: 蓝宝石 # 双子座 # 5 # 月21日 - ------6 # 月21日 # 诞生石: 玛瑙 # 巨蟹座 # 6 # 月22日 - ------7 # 月22日 # 诞生石: 珍珠 # 狮子座 # 7 # 月23日 - ------8 # 月22日 # 诞生石: 红宝石 # 处女座 # 8 # 月23日 - ------9 # 月22日 # 诞生石: 红条纹玛瑙 # 天秤座 # 9 # 月23日 - -----10 # 月23日 # 诞生石: 蓝宝石 # 天蝎座 # 10 # 月24日 - ----11 # 月21日 # 诞生石: 猫眼石 # 射手座 # 11 # 月22日 - ----12 # 月21日 # 诞生石: 黄宝石 # 摩羯座 # 12 # 月22日 - -----1 # 月19日 # 诞生石: 土耳其玉 # 水瓶座 # 1 # 月20日 - ------2 # 月18日 # 诞生石: 紫水晶 # 双鱼座 # 2 # 月19日 - ------3 # 月20日 # 诞生石: 月长石 #对于星座查询我的思路主要是对当月月份具体日期的判断,例如3月中1-20号是天枰座,21-31号是白羊座, #则3月的星座都已筛选出,注意边界值的判断。3月没有32号和0以及-1号,都可归纳于错误输入,给予提示。 #其他月份类似 if month==3: if day>=1 and day<=20: print('你是天枰座的') elif day>=21 and day<=31 : print('你是白羊座的') else: print('输入有误,请重新输入') elif month==4: if day>=1 and day<=19: print('你是白羊座的') elif day>=20 and day<=30: print('你是金牛座的') else: print('输入有误,请重新输入') elif month==5: if day>=1 and day<=20: print('你是金牛座的') elif day>=21 and day<=31: print('你是双子座的') else: print('输入有误,请重新输入') elif month==6: if day>=1 and day<=21: print('你是双子座的') elif day>=22 and day<=30: print('你是巨蟹座的') else: print('输入有误,请重新输入') elif month==7: if day>=1 and day<=22: print('你是巨蟹座的') elif day>=23 and day<=31: print('你是狮子座的') else: print('输入有误,请重新输入') elif month==8: if day>=1 and day<=22: print('你是狮子座的') elif day>=23 and day<=31: print('你是处女座的') else: print('输入有误,请重新输入') elif month==9: if day>=1 and day<=22: print('你是处女座的') elif day>=23 and day<=30: print('你是天枰座的') else: print('输入有误,请重新输入') elif month==10: if day>=1 and day<=23: print('你是天枰座的') elif day>=24 and day <=31: print('你是天蝎座的') else: print('输入有误,请重新输入') elif month==11: if day>=1 and day<=22: print('你是天蝎座的') elif day>=23 and day<=30: print('你是射手座的') else: print('输入有误,请重新输入') elif month==12: if day>=1 and day<=21: print('你是射手座的') elif day>=22 and day<=31: print('你是摩羯座的') else: print('输入有误,请重新输入') elif month==1: if day>=1 and day<=19: print('你是摩羯座的') elif day>=20 and day<=31: print('你是水瓶座的') else: print('输入有误,请重新输入') elif month==2: if day>=1 and day<=18: print('你是水瓶座的') elif day>=19 and day<=29: print('你是天枰座的') else: print('输入有误,请重新输入') #总结:相对于笔者学习的其他语言来说,Python语言使用起来更加方便和高效,但Python虽然省略了其他语言的 #类似 花括号,分号之类的,但是对于空格的格式要求更加精细。要注意!!!