제4문제
자신의 코드는 다음과 같습니다.
1 # -*- coding: utf-8 -*-
2 """
3 Created on Fri Oct 4 22:08:42 2019
4
5 @author: Franz
6 """
7
8 def leap(year):
9 index = False
10 if year % 400 == 0:
11 index = True
12 else:
13 if year % 4 == 0 and year % 100 !=0:
14 index = True
15 return index
16
17 year = int(input('please input the year you want to check: '));
18 month = int(input('please input the month you want to check: '));
19 day = int(input('please input the day you want to check: '))
20
21 mon_days = [0,31,59,90,120,151,181,212,243,273,304,334];
22 if not leap(year):
23 days = mon_days[month-1]+day;
24 else:
25 if month >= 2:
26 days = month_days[month-1]+1+day
27 else:
28 days = mon_days[month-1]+day;
29
30 print('it is the %dth day.' % days)
분석: 코드가 엉망으로 쓰여졌는데 그 중에서 많은 조건 판단은if문장에 합쳐서 판단할 수 있고 코드의 우선순위에 대해 고려하지 않았다.윤년월과 비윤년월의 일수 차이를 깨닫고 한 수조에 따라 월별 조회를 하는 것이 장점이다.
다음은 표준 답안(python-2.7x)입니다.
#!/usr/bin/python
# -*- coding: UTF-8 -*-
year = int(raw_input('year:
'))
month = int(raw_input('month:
'))
day = int(raw_input('day:
'))
months = (0,31,59,90,120,151,181,212,243,273,304,334)
if 0 < month <= 12:
sum = months[month - 1]
else:
print 'data error'
sum += day
leap = 0
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
leap = 1
if (leap == 1) and (month > 2):
sum += 1
print 'it is the %dth day.' % sum
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.