2017-11-13 1 views
-2

저는 현재의 시간과 가까운 계절 이벤트를 계산하는 스크립트를 작성했지만 실제로 확인하는 방법에 대해서는 몇 가지 문제가있었습니다. 그리고 전에 누군가에게 "너는 while 루프를 사용하여 시작을 쉽게 할 수있는 몇 가지 방법이 있다는 것을 알고있다"고 말합니다. - 나도 알아,하지만 그건 내가 묻는 것이 아니야. 실제로 제대로 작동하는 함수를 얻는 방법이 있는지보고 싶습니다.Python에서 Unix를 사용하는 것 이상의 문제가 있습니다.

import datetime 
yearlist = [] 
monthlist = [] 
daylist = [] 
holidaylist = [] 
#all of the time lists correspond to the ammount of years since 1970 - so, for instance, if you type that you were born in 2003, the number of seconds, displayed here 
#will be yearlist[33], or 1041379200 seconds. 
yearlist.append(0)#1970 
yearlist.append(31536000) 
yearlist.append(63072000) 
yearlist.append(94694400) 
yearlist.append(126230400) 
yearlist.append(157766400) 
yearlist.append(189302400) 
yearlist.append(220924800) 
yearlist.append(252460800) 
yearlist.append(283996800) 
yearlist.append(315532800) 
yearlist.append(347155200) 
yearlist.append(378691200) 
yearlist.append(410227200) 
yearlist.append(441763200) 
yearlist.append(473385600) 
yearlist.append(504921600) 
yearlist.append(536457600) 
yearlist.append(567993600) 
yearlist.append(599616000) 
yearlist.append(631152000) 
yearlist.append(662688000) 
yearlist.append(694224000) 
yearlist.append(725846400) 
yearlist.append(757382400) 
yearlist.append(788918400) 
yearlist.append(820454400) 
yearlist.append(852076800) 
yearlist.append(883612800) 
yearlist.append(915148800) 
yearlist.append(946684800) 
yearlist.append(978307200) 
yearlist.append(1009843200) 
yearlist.append(1041379200) 
yearlist.append(1072915200) 
yearlist.append(1104537600) 
yearlist.append(1136073600) 
yearlist.append(1167609600) 
yearlist.append(1199145600) 
yearlist.append(1230768000) 
yearlist.append(1262304000) 
yearlist.append(1293840000) 
yearlist.append(1325376000) 
yearlist.append(1356998400) 
yearlist.append(1388534400) 
yearlist.append(1420070400) 
yearlist.append(1451606400) 
yearlist.append(1483228800) 
yearlist.append(1514764800) 
yearlist.append(1546300800) 
yearlist.append(1577836800) 
yearlist.append(1609459200) 
yearlist.append(1640995200) 
yearlist.append(1672531200) 
yearlist.append(1704067200) 
yearlist.append(1735689600) 
yearlist.append(1767225600) 
yearlist.append(1798761600) 
yearlist.append(1830297600) 
yearlist.append(1861920000) 
yearlist.append(1893456000) 
yearlist.append(1924992000) 
yearlist.append(1956528000) 
yearlist.append(1988150400) 
yearlist.append(2019686400) 
yearlist.append(2051222400) 
yearlist.append(2082758400) 
yearlist.append(2114380800) 

monthlist.append(0)#january 
monthlist.append(2592000)#february 
monthlist.append(5184000)#march 
monthlist.append(7776000)#arpril 
monthlist.append(10368000)#may 
monthlist.append(12960000)#june 
monthlist.append(15552000)#july 
monthlist.append(18144000)#august 
monthlist.append(20736000)#september 
monthlist.append(23328000)#october 
monthlist.append(25920000)#november 
monthlist.append(28512000)#december 


daylist.append(0)#1 
daylist.append(86400)#2 
daylist.append(172800)#3 
daylist.append(259200)#4 
daylist.append(345600)#5 
daylist.append(432000)#6 
daylist.append(518400)#7 
daylist.append(604800)#8 
daylist.append(691200)#9 
daylist.append(777600)#10 
daylist.append(864000)#11 
daylist.append(950400)#12 
daylist.append(1036800)#13 
daylist.append(1123200)#14 
daylist.append(1209600)#15 
daylist.append(1296000)#16 
daylist.append(1382400)#17 
daylist.append(1468800)#18 
daylist.append(1555200)#19 
daylist.append(1641600)#20 
daylist.append(1728000)#21 
daylist.append(1814400)#22 
daylist.append(1900800)#23 
daylist.append(1987200)#24 
daylist.append(2073600)#25 
daylist.append(2160000)#26 
daylist.append(2246400)#27 
daylist.append(2332800)#28 
daylist.append(2419200)#29 
daylist.append(2505600)#30 
daylist.append(2592000)#31 

#holiday list 
holidaylist.append(30931200)#christmas 

file = open("data.txt","a") 
file.write("/Test write." + "\n") 
time.sleep(2) 
seconds=time.time() 
print(seconds) 
timedate=datetime.datetime.fromtimestamp(seconds).strftime('%Y-%m-%d %H:%M:%S:') 
print(timedate) 
file.write(timedate +"\n") 
#time added 

print("Hello, and welcome to the feel better about your life card generator - perfect for all of the depressives in your life.") 
time.sleep(1) 
name=input("OK, first things first. Have a name? ") 
if name=="no": 
    print("...") 
    time.sleep(2) 
    print("Oh.") 
else: 
    print(name + "? Jeez... I feel sorry for you.") 
file.write("name = " + name + "\n") 
    #check if time added 





print("What about your Birthday - I'll be shocked if you don't have one!") 
year=input("What was the year? ") 
#year count starts here, btw 
if year=="1970": 
    birthyear=yearlist[0]+yearlist[0] 
elif year=="1971": 
    birthyear=yearlist[0]+yearlist[1] 
elif year=="1972": 
    birthyear=yearlist[0]+yearlist[2] 
elif year=="1973": 
    birthyear=yearlist[0]+yearlist[3] 
elif year=="1974": 
    birthyear=yearlist[0]+yearlist[4] 
elif year=="1975": 
    birthyear=yearlist[0]+yearlist[5] 
elif year=="1976": 
    birthyear=yearlist[0]+yearlist[6] 
elif year=="1977": 
    birthyear=yearlist[0]+yearlist[7] 
elif year=="1978": 
    birthyear=yearlist[0]+yearlist[8] 
elif year=="1979": 
    birthyear=yearlist[0]+yearlist[9] 
elif year=="1980": 
    birthyear=yearlist[0]+yearlist[10] 
elif year=="1981": 
    birthyear=yearlist[0]+yearlist[11] 
elif year=="1982": 
    birthyear=yearlist[0]+yearlist[12] 
elif year=="1983": 
    birthyear=yearlist[0]+yearlist[13] 
elif year=="1984": 
    birthyear=yearlist[0]+yearlist[14] 
elif year=="1985": 
    birthyear=yearlist[0]+yearlist[15] 
elif year=="1986": 
    birthyear=yearlist[0]+yearlist[16] 
elif year=="1987": 
    birthyear=yearlist[0]+yearlist[17] 
elif year=="1988": 
    birthyear=yearlist[0]+yearlist[18] 
elif year=="1989": 
    birthyear=yearlist[0]+yearlist[19] 
elif year=="1990": 
    birthyear=yearlist[0]+yearlist[20] 
elif year=="1991": 
    birthyear=yearlist[0]+yearlist[21] 
elif year=="1992": 
    birthyear=yearlist[0]+yearlist[22] 
elif year=="1993": 
    birthyear=yearlist[0]+yearlist[23] 
elif year=="1994": 
    birthyear=yearlist[0]+yearlist[24] 
elif year=="1995": 
    birthyear=yearlist[0]+yearlist[25] 
elif year=="1996": 
    birthyear=yearlist[0]+yearlist[26] 
elif year=="1997": 
    birthyear=yearlist[0]+yearlist[27] 
elif year=="1998": 
    birthyear=yearlist[0]+yearlist[28] 
elif year=="1999": 
    birthyear=yearlist[0]+yearlist[29] 
elif year=="2000": 
    birthyear=yearlist[0]+yearlist[30] 
elif year=="2001": 
    birthyear=yearlist[0]+yearlist[31] 
elif year=="2002": 
    birthyear=yearlist[0]+yearlist[32] 
elif year=="2003": 
    birthyear=yearlist[0]+yearlist[33] 
elif year=="2004": 
    birthyear=yearlist[0]+yearlist[34] 
elif year=="2005": 
    birthyear=yearlist[0]+yearlist[35] 
elif year=="2006": 
    birthyear=yearlist[0]+yearlist[36] 
elif year=="2007": 
    birthyear=yearlist[0]+yearlist[37] 
elif year=="2008": 
    birthyear=yearlist[0]+yearlist[38] 
elif year=="2009": 
    birthyear=yearlist[0]+yearlist[39] 
elif year=="2010": 
    birthyear=yearlist[0]+yearlist[40] 
elif year=="2011": 
    birthyear=yearlist[0]+yearlist[41] 
elif year=="2012": 
    birthyear=yearlist[0]+yearlist[42] 
elif year=="2013": 
    birthyear=yearlist[0]+yearlist[43] 
elif year=="2014": 
    birthyear=yearlist[0]+yearlist[44] 
elif year=="2015": 
    birthyear=yearlist[0]+yearlist[45] 
elif year=="2016": 
    birthyear=yearlist[0]+yearlist[46] 
elif year=="2017": 
    birthyear=yearlist[0]+yearlist[47] 
elif year=="2018": 
    birthyear=yearlist[0]+yearlist[48] 
elif year=="2019": 
    birthyear=yearlist[0]+yearlist[49] 
elif year=="2020": 
    birthyear=yearlist[0]+yearlist[50] 
elif year=="2021": 
    birthyear=yearlist[0]+yearlist[51] 
elif year=="2022": 
    birthyear=yearlist[0]+yearlist[52] 
elif year=="2023": 
    birthyear=yearlist[0]+yearlist[53] 
elif year=="2024": 
    birthyear=yearlist[0]+yearlist[54] 
elif year=="2025": 
    birthyear=yearlist[0]+yearlist[55] 
elif year=="2026": 
    birthyear=yearlist[0]+yearlist[56] 
elif year=="2027": 
    birthyear=yearlist[0]+yearlist[57] 
elif year=="2028": 
    birthyear=yearlist[0]+yearlist[58] 
elif year=="2029": 
    birthyear=yearlist[0]+yearlist[59] 
elif year=="2030": 
    birthyear=yearlist[0]+yearlist[60] 
else: 
    print("Damn, you're getting so old that even unix doesn't support your existence.") 
    time.sleep(1) 
    print("What else can I say? I'm not gonna make the counter go negative, that'll make me have to re-write most of this.") 
    time.sleep(2) 
    print("Go on then, leave. You broke it by being old. Congrats.") 


time.sleep(1) 

month=input("What month? ") 
if month=="january": 
    birthyearmonth = birthyear+monthlist[0] 
    birthmonth = 0 + monthlist[0] 
elif month=="february": 
    birthyearmonth = birthyear+monthlist[1] 
    birthmonth = 0 + monthlist[1] 
elif month=="march": 
    birthyearmonth = birthyear+monthlist[2] 
    birthmonth = 0 + monthlist[2] 
elif month=="april": 
    birthyearmonth = birthyear+monthlist[3] 
    birthmonth = 0 + monthlist[3] 
elif month=="may": 
    birthyearmonth = birthyear+monthlist[4] 
    birthmonth = 0 + monthlist[4] 
elif month=="june": 
    birthyearmonth = birthyear+monthlist[5] 
    birthmonth = 0 + monthlist[5] 
elif month=="july": 
    birthyearmonth = birthtyear+monthlist[6] 
    birthmonth = 0 + monthlist[6] 
elif month=="august": 
    birthyearmonth = birthyear+monthlist[7] 
    birthmonth = 0 + monthlist[7] 
elif month=="september": 
    birthyearmonth = birthyear+monthlist[8] 
    birthmonth = 0 + monthlist[8] 
elif month=="october": 
    birthyearmonth = birthyear+monthlist[9] 
    birthmonth = 0 + monthlist[9] 
elif month=="november": 
    birthyearmonth = birthyear+monthlist[10] 
    birthmonth = 0 + monthlist[10] 
elif month=="december": 
    birthyearmonth = birthyear+monthlist[11] 
    birthmonth = 0 + monthlist[11] 
else: 
    time.sleep(1) 
    print("...of course you mistyped something. Typical.") 
    time.sleep(1) 
    print("Right, reset the shell - c'mon, this was you're fault.") 
#That's the end of the month and year thing, future Laz. 

time.sleep(1) 
print("So - thus far we've got the year, and month. Logically, next is the day.") 
time.sleep(2) 
day=input("So, what date was it? ") 
#here's the start of the final hurdle, Laz - don't mess this up. 
if day=="1": 
     birthfull = birthyearmonth+1+daylist[0] 
     birthmonthday = birthmonth+daylist[0] 
elif day=="2": 
     birthfull = birthyearmonth+daylist[1] 
     birthmonthday = birthmonth+daylist[1] 
elif day=="3": 
     birthfull = birthyearmonth+daylist[2] 
     birthmonthday = birthmonth+daylist[2] 
elif day=="4": 
     birthfull = birthyearmonth+daylist[3] 
     birthmonthday = birthmonth+daylist[3] 
elif day=="5": 
     birthfull = birthyearmonth+daylist[4] 
     birthmonthday = birthmonth+daylist[4] 
elif day=="6": 
     birthfull = birthyearmonth+daylist[5] 
     birthmonthday = birthmonth+daylist[5] 
elif day=="7": 
     birthfull = birthyearmonth+daylist[6] 
     birthmonthday = birthmonth+daylist[6] 
elif day=="8": 
     birthfull = birthyearmonth+daylist[7] 
     birthmonthday = birthmonth+daylist[7] 
elif day=="9": 
     birthfull = birthyearmonth+daylist[8] 
     birthmonthday = birthmonth+daylist[8] 
elif day=="10": 
     birthfull = birthyearmonth+daylist[9] 
     birthmonthday = birthmonth+daylist[9] 
elif day=="11": 
     birthfull = birthyearmonth+daylist[10] 
     birthmonthday = birthmonth+daylist[10] 
elif day=="12": 
     birthfull = birthyearmonth+daylist[11] 
     birthmonthday = birthmonth+daylist[11] 
elif day=="13": 
     birthfull = birthyearmonth+daylist[12] 
     birthmonthday = birthmonth+daylist[12] 
elif day=="14": 
     birthfull = birthyearmonth+daylist[13] 
     birthmonthday = birthmonth+daylist[13] 
elif day=="15": 
     birthfull = birthyearmonth+daylist[14] 
     birthmonthday = birthmonth+daylist[14] 
elif day=="16": 
     birthfull = birthyearmonth+daylist[15] 
     birthmonthday = birthmonth+daylist[15] 
elif day=="17": 
     birthfull = birthyearmonth+daylist[16] 
     birthmonthday = birthmonth+daylist[16] 
elif day=="18": 
     birthfull = birthyearmonth+daylist[17] 
     birthmonthday = birthmonth+daylist[17] 
elif day=="19": 
     birthfull = birthyearmonth+daylist[18] 
     birthmonthday = birthmonth+daylist[18] 
elif day=="20": 
     birthfull = birthyearmonth+daylist[19] 
     birthmonthday = birthmonth+daylist[19] 
elif day=="21": 
     birthfull = birthyearmonth+daylist[20] 
     birthmonthday = birthmonth+daylist[20] 
elif day=="22": 
     birthfull = birthyearmonth+daylist[21] 
     birthmonthday = birthmonth+daylist[21] 
elif day=="23": 
     birthfull = birthyearmonth+daylist[22] 
     birthmonthday = birthmonth+daylist[22] 
elif day=="24": 
     birthfull = birthyearmonth+daylist[23] 
     birthmonthday = birthmonth+daylist[23] 
elif day=="25": 
     birthfull = birthyearmonth+daylist[24] 
     birthmonthday = birthmonth+daylist[24] 
elif day=="26": 
     birthfull = birthyearmonth+daylist[25] 
     birthmonthday = birthmonth+daylist[25] 
elif day=="27": 
     birthfull = birthyearmonth+daylist[26] 
     birthmonthday = birthmonth+daylist[26] 
elif day=="28": 
     birthfull = birthyearmonth+daylist[27] 
     birthmonthday = birthmonth+daylist[27] 
elif day=="29": 
     birthfull = birthyearmonth+daylist[28] 
     birthmonthday = birthmonth+daylist[28] 
elif day=="30": 
     birthfull = birthyearmonth+daylist[29] 
     birthmonthday = birthmonth+daylist[29] 
elif day=="31": 
     birthfull = birthyearmonth+daylist[30] 
     birthmonthday = birthmonth+daylist[30] 
print(birthmonthday) 
#Oooh, that was WAY harder to write than the year list. 
if name=="admin": 
    print("Whoops, I should've removed this - BUT SINCE IT HAS ACTUAL PURPOSE I GUESS NOT") 
else: 
    datetime1970 = int(seconds - yearlist[47]) 
    christmasfinal = holidaylist[0] - datetime1970 
    print(christmasfinal) 


birthactual=datetime.datetime.fromtimestamp(birthfull).strftime('%Y-%m-%d %H:%M:%S:') 
print(birthactual) 


if birthmonthday>christmasfinal: 
    print("Christmas is closer. Also you gotta work on that card") 


elif birthmonthday<christmasfinal: 
    print("Birthday's closer, ya dingus") 

그리고 나는이 사실을 알고 있습니다.

그리고 이것이 내 첫번째 파이썬 프로젝트입니다. 실제로 보여줍니다.

편집 : 코드의 문제점은 제공된 날짜가 아니더라도 크리스마스가 더 가깝다는 것을 끊임없이 알려주는 것입니다. 또한 최종 생년월일은 완벽하지는 않지만 며칠이지만 사용자에게 중요하지 않아야한다고 생각합니다. 명확히하지 않으면 죄송합니다.

+0

이 모든 'if'문을 작성하는 데 얼마나 오랜 시간이 걸렸을 지 상상조차 할 수 없습니다. 소품들. – kstullich

+0

나는 내 첫 프로그램을 기억합니다. 그것은 끔찍 했어요. 어, 당신은 정말로 루프를 사용하는 방법을 조사해야합니다. –

+3

눈이 피고 있습니다. –

답변

0

올바른 값을 비교하고 있지 않습니다. birthmonthday은 연초부터 사용자 생일까지의 시간 (초)입니다. christmasfinal은 지금부터 크리스마스까지의 시간 (초)입니다. 그것들을 서로 비교하는 것은 의미가 없습니다. 어느 쪽이 더 가까운 지 알고 싶으면 birthmonthdaydatetime1970에서 뺍니다 (이 변수의 이름은 1970 년이 아닌 2017 년 초부터의 초 수이기 때문에 매우 이상합니다). 그리고이를 christmasfinal과 비교하십시오. 또한 차이의 절대 값을 사용해야합니다. 하나는 음수이고 다른 하나는 양수 일 수 있기 때문입니다.

seconds_to_birthday = birthmonthday - datetime1970 
if (abs(seconds_to_birthday) > abs(christmasfinal)): 
    print("Christmas is closer. Also you gotta work on that card") 
elif (abs(seconds_to_birthday) < abs(christmasfinal)): 
    print("Birthday's closer, ya dingus") 
else: 
    print("We're exactly halfway between birthday and Christmas, you decide what to do") 
관련 문제