2016-10-26 7 views
0

私は、ユーザに日付(日、月、年)を求め、曜日(月曜日、火曜日など)に答えを返すプログラムを書いています。 https://es.wikibooks.org/wiki/Algoritmia/Algoritmo_para_calcular_el_d%C3%ADa_de_la_semana このエラーを取得 - 私:アルゴリズムpythonで平日を計算する

ファイル "C:/ユーザー/ USUARIO /ドキュメント/Programación/ Desafio 4 /ワルド・ムニョスのdesafio 4 /ディア・デ・ラ・semana55.py"、ライン64彼のアルゴリズムによると、 、in Algoritmo =((年 - 1)%7 +((年 - 1)/ 4 - 3 *((年 - 1)/ 100 + 1)/ 4)%7 +月+日%7)%7

はTypeError:+のためのサポートされていないオペランドのタイプ(S): 'フロート' と 'str' は

これは私がこれまで持っているものです。

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") 

PS:私はスペイン語のネイティブスピーカーです。間違いがあれば申し訳ありません。

+3

のようなのelifがあれば、計算の結果によって日数とインデックス作成これでリストを作ることによって数行に減少させることができるの長鎖ですか? –

+0

申し訳ありません...私はこれを得ています: ファイル "C:/ユーザー/ USUARIO /ドキュメント/Programación/ Desafio 4/WaldoMuñozdesafio 4/Dia de la semana55.py"、行64、 Algoritmo = (年 - 1)%7 +((年 - 1)/ 4 - 3 *((年 - 1)/ 100 + 1)/ 4)%7 +月+日%7)%7 TypeError:サポートされていません+: 'float'と 'str'のオペランドのタイプ – Teddy

+2

月の場合は '' month == january ''の場合は引用符がありません: – techydesigner

答えて

0

エラーを起こさないでください。訂正またはその他のコードを停止し、それはあなたがその旨、あなたのために、正しい月がユーザによって導入されていることを確認する必要があるインスタンス

>>> 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' 
>>> 

ため

を参照してくださいエラーを引き起こす文字列として残りますこのようなことができます

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 

も、あなたはその値を保持する新しい変数を作成し、代わりにそれを使用このあまり

(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)) 

を繰り返します。

はまた、あなたの質問は何例えば

days_names=["Monday", "Tuesday", ... ] 
print(days_names[Algoritmo]) 
+0

ヒントをお願いします! – Teddy