Estoy haciendo una tarea en Python, que es la creación de un calendario del zodiaco a juzgar por el mes de entrada y día del mes.
Yo he hecho un calendario de éxito, sin embargo, no tengo idea de cómo resolver el problema, si la entrada I un nombre incorrecto mes o la fecha inexistente.
He probado un montón de cosas: crear una jerarquía diferente, o bajo cada signo del zodiaco que me escribió else: print ('Entering an incorrect date')
. Incluso he intentado registrarse con if day <0 and day> 31 or month! = 'March' or month! = 'April'
, etc ... resultó ser una gran función inútil en el final, y nada funcionó bien.
Siento que hay una solución de una línea para detectar días <0 y> 31 y nombre incorrecto meses, pero simplemente no puedo entenderlo. Podría alguien ayuda, por favor?
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)
Es posible que desee ver en la fecha y hora del módulo. Con él, usted puede hacer comparaciones entre fechas y horas fácilmente sin la necesidad de extraños si las declaraciones.
Por ejemplo, si uno fuera a hacer algo como:
import datetime
dt = datetime.datetime.today()
print dt.month
A continuación, se obtendría:
3
Así que en lugar de la lectura en la entrada durante meses en un formato de cadena, simplemente puede pedir el número del mes para ayudar a crear una fecha y hora de objetos con mayor facilidad. Como alternativa, puede hacer algo como esto:
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
De esa manera usted puede obtener el número del mes de utilizar una fecha y hora objeto de hacer sus comparaciones y evaluaciones de allí.