I´m writing a program that asks the user a date (day, month and year) and you get as answer the day of the week (Monday, Tuesday, etc.). According to his Algorithm: https://es.wikibooks.org/wiki/Algoritmia/Algoritmo_para_calcular_el_d%C3%ADa_de_la_semana I´m getting this error:
File "C:/Users/USUARIO/Documents/Programación/Desafio 4/Waldo Muñoz desafio 4/Dia de la semana55.py", line 64, in Algoritmo = ((year - 1) % 7 + ((year - 1) / 4 - 3 * ((year - 1) / 100 + 1) / 4) % 7 + month + day % 7) % 7
TypeError: unsupported operand type(s) for +: 'float' and 'str'
This is what I have so far:
day = int(input("Day of the month (number): "))
month = input("Name of the month: ")
month = month.lower()
year = int(input("The year is (numbers): "))
#In order to calculate the day of the week (Monday, ,Tuestday,etc)
#There are two cases: Leap year and non-leap.
if month == "january":
month = 0
elif month == "february":
month = 3
#These two months have equal module in leap year and non-leap.
elif month == "march":
month = 3 #non-leap
if(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)): #condition to be leap
month = 4
elif month == "april":
month = 6
if(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
month = 0
elif month == "may":
month = 1
if(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
month = 2
elif month == "june":
month = 4
if(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
month = 5
elif month == "july":
month = 6
if(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
month = 0
elif month == "august":
month = 2
if(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
month = 3
elif month == "september":
month = 5
if(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
month = 6
elif month == "october":
month = 0
if(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
month = 1
elif month == "november":
month = 3
if(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
month = 4
elif month == "december":
month = 5
if(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
month = 6
else:
print("Please, write the date with the correct format.")
Algoritmo = int((year - 1) % 7 + ((year - 1) / 4 - 3 * ((year - 1) / 100 + 1) / 4) % 7 + month + day % 7) % 7
#Algorithm to calculate day of the week
if Algoritmo == 0:
print ("Monday")
elif Algoritmo == 1:
print ("Tuesday")
elif Algoritmo == 2:
print ("Wednesday")
elif Algoritmo == 3:
print ("Thursday")
elif Algoritmo == 4:
print ("Friday")
elif Algoritmo == 5:
print ("Saturday")
elif Algoritmo == 6:
print ("Sunday")
P.S.: I am a native spanish speaker, I'm sorry if there are mistakes...
the error is most likely when you misspelled a month, as you don't trow a error or ask for a correction or otherwise stop the code, it remain as a string which cause the error you see
for instance
>>> test()
Day of the month (number): 25
Name of the month: october
The year is (numbers): 2016
Tuesday
>>> test()
Day of the month (number): 25
Name of the month: octubre
The year is (numbers): 2016
Please, write the date with the correct format.
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
test()
File "C:\Users\David\Documents\Python Scripts\stackoverflow_test.py", line 67, in test
Algoritmo = int((year - 1) % 7 + ((year - 1) / 4 - 3 * ((year - 1) / 100 + 1) / 4) % 7 + month + day % 7) % 7
TypeError: unsupported operand type(s) for +: 'float' and 'str'
>>>
you need to make sure that a correct month is introduced by the user, for that effect you can do something like this
months = {"january","february",...}#put all the month here
def ask_month():
result=""
while result not in months:
result = input("Name of the month: ").lower()
return result
also you repeat this too much
(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0))
make a new variable which hold that value, and use that instead.
Also that long chain of if-elif can be reduced to a few lines by making a list with the days and indexing it by the result of the calculation like for example
days_names=["Monday", "Tuesday", ... ]
print( days_names[Algoritmo] )