使用循环时将用户输入存储到列表中

时间:2018-02-14 17:50:18

标签: python list loops variables nested-loops

想法是让用户输入日期,并让程序执行3个功能。第一个函数计算出是否是闰年,然后根据一年中的多少天计算年份数(例如:日期1-1-2008年第1号)这部分我已经完成并且工作。第二个功能是打印日期和年份编号,但我也无法弄清楚如何存储旧条目。第三个函数将文件和每个条目保存在单独的行中。我知道我对代码的保存组件有问题 - 但我只是想完成第二部分,打印所有条目的结果,然后继续处理其他任何事情。所以我的问题是,如何存储用户输入的所有输入,而不仅仅是输入的最后一个输入。

Expected inputs= M-D-YYYY
ex:, 3-2-2008, 3-2-2010, 1-2-2017
Expected output 
Date Entered     Date Number
3-2-2008                  62
3-2-2010                  61
1-2-2017                   2

def calc_day_num_of_year(x):
    global userInput
    userInput=x
    dateSplit=x.split('-')
    month=dateSplit[0]
    monthInt=int(month)
    day=dateSplit[1]
    dayInt=int(day)
    year=dateSplit[2]
    yearInt=int(year)
    while yearInt%4==0 and yearInt%100!=0:
        if monthInt==1:
            dateNum=0+dayInt
            break
        elif monthInt==2:
            dateNum=31+dayInt
            break
        elif monthInt == 3:
            dateNum=60+dayInt
            break
        elif monthInt == 4:
            dateNum=91+dayInt
            break
        elif monthInt == 5:
            dateNum=121+dayInt
            break
        elif monthInt == 6:
            dateNum=152+dayInt
            break
        elif monthInt == 7:
            dateNum=182+dayInt
            break
        elif monthInt == 8:
            dateNum=213+dayInt
            break
        elif monthInt == 9:
            dateNum=244+dayInt
            break
        elif monthInt == 10:
            dateNum=274+dayInt
            break
        elif monthInt == 11:
            dateNum=305+dayInt
            break
        elif monthInt == 12:
            dateNum=335+dayInt
            break

    else:
        if monthInt == 1:
            dateNum=0+dayInt
        elif monthInt == 2:
            dateNum=31+dayInt
        elif monthInt == 3:
            dateNum=59+dayInt
        elif monthInt == 4:
            dateNum=90+dayInt
        elif monthInt == 5:
            dateNum=120+dayInt
        elif monthInt == 6:
            dateNum=151+dayInt
        elif monthInt == 7:
            dateNum=181+dayInt
        elif monthInt == 8:
            dateNum=212+dayInt
        elif monthInt == 9:
            dateNum=243+dayInt
        elif monthInt == 10:
            dateNum=273+dayInt
        elif monthInt == 11:
            dateNum=304+dayInt
        elif monthInt == 12:
            dateNum=334+dayInt
    global strDateNum
    strDateNum=str(dateNum)

def display_day_num_of_year(y):
    global dataList
    dataList= '{:10}{:>14}'.format(userInput,strDateNum)
    print(dataList)

def save_day_num_of_year(z):
    with open ('outputData.txt','w') as outFile:
        for i in range (len(dataList)):
            global outString
            outString=' ' 
            outString+= str(dataList(i))
            outString+='\n'
            print(out_string)
            out_file.write(out_string)

while True:
    date=input('Please enter date numerically in form month-day-year: ')
    if date =='':
        heading= '{:10}{:14}'.format('Date_Entered ','Date_Number')
        print(heading)
        display_day_num_of_year(date)
        heading
        save_day_num_of_year(date)
    else:
        calc_day_num_of_year(date)

1 个答案:

答案 0 :(得分:0)

两个模块可以解决您的问题(如果您可以使用它们):

import calendar
import datetime

def display(d, ly, doy):
    print ('{:12} {:10} {:10}'.format(d, str(ly), doy))

uin = tuple() # user inputs

while True:
    d = input('Enter date in the format YYYY MM DD: ').split()
    if len(d) == 0:
        break # no date given exit the 'while'
    print ('{:12} {:10} {:10}'.format('Date_Entered ', 'Leap_Year', 'Day_Of_Year'))
    ly = calendar.isleap(d[0])
    try:
        dd = datetime.date(int(d[0]), int(d[1]), int(d[2]))
        doy = dd.timetuple().tm_yday # get the day of year
    except ValueError:
        doy = 'N/A' # date was not a valid date
    display(d), ly, doy)
    uin += tuple([tuple(d)]) # update inputs

回答关于存储用户输入的问题

uin = tuple()
while True:
    d = input('Enter date in the format YYYY MM DD: ').split()
    # rest of code
    uin += tuple([tuple(d)]) # make immutable and concatenate tuples