2017-11-13 15 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") 

はい、私はこれでひどいことを知っています。

はい、これは私の最初のPythonプロジェクトです。実際これが示しています。

EDIT:コードの問題は、提供された日付がそうでなくても、クリスマスがより近いことを常に教えてくれることです。また、私は最終的な生年月日が完璧ではない(数日外)が、ユーザーにとって重要ではないほど十分に近いと言います。申し訳ございません。

+0

これらのすべての 'if'文を書くのにどれくらいの時間がかかるか想像もできません。あなたに小道具。 – kstullich

+0

私は初めてのプログラムを覚えています。それは恐ろしいことだった。あなたは本当にループを使うべきだと思うべきです。 –

+3

私の目は出血しています。 –

答えて

0

あなたは正しい値を比較していません。 birthmonthdayは、年の初めからユーザーの誕生日までの秒数です。 christmasfinalは、今からクリスマスまでの秒数です。これらを互いに比較することはほとんど意味がありません。どちらが近いかを知りたい場合はをdatetime1970から差し引く必要があります(この変数の名前は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") 
関連する問題