#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Help me out with this code


    Hey I've been struggling to get my head around python programming and I've been given an assignment that I just cant get right, any help would be awesome!
    its got to read a inputted txt file thats full of months and average temperatures for each day in the month.
    looks like this except it goes through to december:
    January 32 33 34 35 33 31 32 33 34 35 36 37 38 39 40 41 42 41 40 38 36 34 33 35 37 36 37 38 39 40 40

    After that i have to count how many Cool, Hot and Very Hot there are for each month.
    Cool is <30
    Hot is between 30 and 38
    Very hot is >38
    then write the results to another separate file defined by the user.
    Im stuck and cant get any of it to run.
    here is the code I have so far any help would be awesome:
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0
    Here it is:


    import string

    def main():
    infilename = raw_input( 'Enter the file name: ' )
    outfilename = raw_input( 'Enter the file name you want the processed data to save to: ')

    #open the files
    infile = open( infilename, 'r' ) # this opens inputted file above
    outfile = open( outfilename, 'w') # opens output file
    dataStr = infile.read() #reading the files
    dataStr = dataStr.split('\n') #splitting the data up



    for line in len(dataStr):
    #print line
    line = line.split( ' ')
    #print splitline
    month = line[0] #defined what part of the file refers to the month
    day = line[1:] #defined what part of the file refers to the day

    c = 0
    h = 0
    vh = 0
    #for i in range()
    #if i <30: c + 1
    #elif i >38: vh + 1
    #else: h + 1

    #temp =


    outfile.write( temp + '\n' )
    outfile.close
    infile.close
    main()
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    Should we do your homework? Yes, so you get a model. Will your professor provide a model? Maybe. Is ours better? Probably.
    Code:
    import sys
    if sys.version[0] == '3':
        raw_input = input
    
    def classify(temperatures):
        '''
            invoke this doc test with shell command
            python -m doctest thisfile.py
    
            >>> classify(list(range(40)))
            [31, 8, 1]
        '''
        counts = [0, 0, 0]
        for temperature in temperatures:
            counts[(30 < temperature) + (38 < temperature)] += 1
        return counts
    
    def main():
        infilename = raw_input( 'Enter the file name: ' )
        infile = open( infilename, 'r' ) # fail as soon as possible.  Don't bother to ask about the output if there isn't a valid input.
        outfilename = raw_input( 'Enter the file name you want the processed data to save to: ')
        outfile = open( outfilename, 'w')
        for line in infile: # text files open for read are iterators by line
            words = line.split( ' ')  # use a new variable name
            month = words[0]
            temperatures = words[1:]  # temperatures is a list of strings.
            integer_temperature_iterable = map(int,temperatures) # mechanism to convert temperature to number
            counts_by_category = classify(integer_temperature_iterable)
            outfile.write(month + ' ' + str(list(map(str,counts_by_category))) + '\n')
        infile.close()
        outfile.close()
    
    if __name__ == '__main__':
        main()
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo