Python 十二生肖日历样本中日期的意外输入值检测
我正在用Python做一项任务,就是根据输入的月份和月份的日期创建一个黄道带日历 我已经制作了一个成功的日历,但是,如果我输入了错误的月份名称或不存在的日期,我不知道如何解决这个问题 我已经尝试了很多事情:创建一个不同的层次结构,或者在我写的每个星座下Python 十二生肖日历样本中日期的意外输入值检测,python,python-3.x,Python,Python 3.x,我正在用Python做一项任务,就是根据输入的月份和月份的日期创建一个黄道带日历 我已经制作了一个成功的日历,但是,如果我输入了错误的月份名称或不存在的日期,我不知道如何解决这个问题 我已经尝试了很多事情:创建一个不同的层次结构,或者在我写的每个星座下else:print('输入错误的日期')。我甚至试着注册如果第31天或第31个月三月还是月份四月号,等等。。。结果证明它最终是一个巨大的无用函数,而且也没有任何效果 我觉得这里有一个单行解决方案可以检测31天以及不正确的月份名称,但我就是不明白。
else:print('输入错误的日期')
。我甚至试着注册如果第31天或第31个月三月还是月份四月号
,等等。。。结果证明它最终是一个巨大的无用函数,而且也没有任何效果
我觉得这里有一个单行解决方案可以检测<0天和>31天以及不正确的月份名称,但我就是不明白。有人能帮忙吗
month = input('Enter the month: ')
day = int(input ('Enter the date of month: '))
if day >= 21 and day <= 31 and month.lower() == 'march' or day >=1 and day <= 19 and month.lower() == 'april':
zodiac='Aries'
if day >= 20 and day <= 30 and month.lower() == 'april' or day >=1 and day <= 19 and month.lower() == 'may':
zodiac='Taurus'
if day >= 21 and day <= 31 and month.lower() == 'may' or day >=1 and day <= 20 and month.lower() == 'june':
zodiac='Gemini'
if day >= 21 and day <= 30 and month.lower() == 'june' or day >=1 and day <= 22 and month.lower() == 'july':
zodiac='Cancer'
if day >= 23 and day <= 31 and month.lower() == 'july' or day >=1 and day <= 22 and month.lower() == 'august':
zodiac='Leo'
if day >= 23 and day <= 31 and month.lower() == 'august' or day >=1 and day <= 22 and month.lower() == 'september':
zodiac='Virgo'
if day >= 23 and day <= 30 and month.lower() == 'september' or day >=1 and day <= 22 and month.lower() == 'october':
zodiac='Libra'
if day >= 23 and day <= 31 and month.lower() == 'october' or day >=1 and day <= 21 and month.lower() == 'november':
zodiac='Scorpio'
if day >= 22 and day <= 30 and month.lower() == 'november' or day >=1 and day <= 21 and month.lower() == 'december':
zodiac='Sagittarius'
if day >= 22 and day <= 31 and month.lower() == 'december' or day >=1 and day <= 19 and month.lower() == 'january':
zodiac='Capricorn'
if day >= 20 and day <= 31 and month.lower() == 'january' or day >=1 and day <= 18 and month.lower() == 'february':
zodiac='Aquarius'
if day >= 19 and day <= 28 and month.lower() == 'february' or day >=1 and day <= 20 and month.lower() == 'march':
zodiac='Pisces'
print('Conclusion:')
print(zodiac)
month=input('输入月份:')
day=int(输入('输入月份的日期:'))
如果天>=21,天=1,天=20,天=1,天=21,天=1,天=23,天=1,天=23,天=1,天=23,天=1,天=23,天=1,天=22,天=1,天=22,天=1,天=20,天=1,天=19,天=1,天您可能需要查看该模块。使用它,您可以轻松地在日期和时间之间进行比较,而不需要额外的if语句
例如,如果一个人要做以下事情:
import datetime
dt = datetime.datetime.today()
print dt.month
然后你会得到:
3
因此,与以字符串格式读取输入数月不同,您可以简单地请求月数,以帮助更轻松地创建对象。或者,您可以这样做:
month_input = input("What is the month?")
months = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]
month_input = month_input.lower().strip() #convert to lowercase and remove whitespace
month_num = months.index(month_input) + 1 #Add one because lists start at index 0
这样,您就可以从中获取使用对象进行比较和评估的月份数。其他问题是什么:打印(…)
?您应该尝试使用标准库中的模块。例如,见