본문 바로가기
IT 이것저것/파이썬python

파이썬python 요일 계산 프로그램을 짜 보았다. - 완성편

by KaNonx카논 2017. 5. 2.
반응형

파이썬python 요일 계산 프로그램을 짜 보았다. - 완성편

 

안녕하세요!

 

이번엔 전 시간에 이어서 파이썬으로 요일 계산 프로그램을 완성시켜 볼 겁니다!

 

2017/04/27 - 파이썬python 요일 계산 프로그램을 짜 보았다.

 

전 시간에 요일 프로그램을 작동시키는데에는 성공했지요!

 

하지만, 논리적 오류라는 결함으로 제대로 작동에 실패했습니다.

 

그리고 제대로 요일도 나오지 않았지요!

 

오늘을 논리적 오류도 해결하고

 

요일도 제대로 표시하도록 해 보겠습니다~~!!

 

 

우선 전 시간의 프로그램의 짜임새를 볼까요!

 


month = int(input("월을 입력하시오 : "))
day = int(input("일을 입력하시오 : "))
month_days = [0,31,28,31,30,31,30,31,30,31,30,31,30]
total_days = 0

for year_item in range(1, year):
    total_days = total_days + 365
     
    if year_item % 400 == 0:
        total_days = total_days + 1           
    elif year_item % 100 == 0:
        pass           
    elif year_item % 4 == 0:
        total_days = total_days + 1           
    else:
        pass
        
for month_index in range(1, month):
    total_days = total_days + month_days[month_index]
   
total_days = total_days + day

print(year,"년",month,"월",day,"일", total_days)
print(total_days % 7)

 

여기에서 오류는 바로 굵은 글씨의 저 곳이었습니다!


올해가 윤년이며 달이 3월달 이후라면 하루를 더하여야 되므로

 

if year % 400 == 0 and month >= 3:
 total_days = total_days + 1
elif year_item % 100 == 0:
 pass         
elif year % 4 == 0 and month >= 3:
 total_days = total_days + 1
else:
 pass

 

라는 코드를 살며시 더해주면 됩니다!!

 

이로써 논리적인 오류는 해 to the 결~~!!!

 

 

이후에 요일 표시는 간단하지요!

 

 

if total_days % 7 == 0:
    print("일요일")
elif total_days % 7 == 1:
    print("월요일")
elif total_days % 7 == 2:
    print("화요일")
elif total_days % 7 == 3:
    print("수요일")
elif total_days % 7 == 4:
    print("목요일")
elif total_days % 7 == 5:
    print("금요일")
elif total_days % 7 == 6:
    print("토요일")

 

total_days가 0으로 나뉘어 떨어지면 0으로 일요일

 

total_days을 7로 나누어 1이 남으면 월요일

 

.

.

.

 

이렇게 요일을 손쉽게 출력 할 수 있습니다!

 

어때요 참 쉽죠~~?? ㅋㅋㅋㅋ

 

-

 

 

 

 

 

반응형

댓글