Weather calculation program - TypeError: int()

For students who are taking a programming course, may be restricted in what they can use, or have other unusual limitations on solving their task.

Weather calculation program - TypeError: int()

Postby informatics109 » Wed Sep 14, 2016 3:44 pm

Hello, there,
Im trying to create a program which reads a .txt file, asks the user to input a specific year, calculates the average monthly temperature(TAM) and monthly wind speed(FXM) and then writes these calculations to a new file.

This is what I have so far:
Code: Select all
def avg1():
    filein = input("Please enter the name of the file: ")
    fileout = input("Please enter the new file name: ")
    infile = open(filein,'r')
    outfile = open(fileout,'w')
    year =(int(input("Please enter the year: ")))
    count=-1
    temp=[[] for i in range(12)]
    wind=[[] for i in range(12)]
    f = infile.readlines()
    with open(filein,'r') as f:
        for line in f:
            kolonne = line.split('\t')
            temp.append(kolonne[8])
            temp1 = int(temp)
            wind.append(kolonne[6])
            wind1 = int(wind)
            dato = kolonne[1]
            dato = dato.split('.')
            aar = int(dato[2])
            month = int(dato[1])
        print("Dato\t\tTAM\t\tFXM")
        print(month+aar,round(sum(temp)/len(temp),2))
        print(month+aar,round(sum(wind)/len(wind),2))

When I run this program I get an error "TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'" at line 15. Im stuck, haven't been able to move forward for a while. Any help is greatly appreciated!
Attachments
dat.PNG
This is how the data looks like
dat.PNG (52.18 KiB) Viewed 926 times
Last edited by Yoriz on Wed Sep 14, 2016 4:00 pm, edited 2 times in total.
Reason: First post lock. Added code tags. Added error to subject.
informatics109
 
Posts: 3
Joined: Wed Sep 14, 2016 3:34 pm

Re: Weather calculation program - TypeError: int()

Postby Yoriz » Wed Sep 14, 2016 4:08 pm

The code is trying to apply the int function on a list object which is not possible.

temp and wind list are list of empty lists, you can just create an empty list and append each item to it.
instead of
Code: Select all
temp=[[] for i in range(12)]

just use
Code: Select all
temp=[]

when you then append items to a list it will grow as needed.

You mean to apply int to the result of splitting a line in the file and then append that to the list
instead of
Code: Select all
temp.append(kolonne[8])
temp1 = int(temp)

apply int to the value appended to the list
Code: Select all
temp.append(int(kolonne[8]))
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Yoriz
 
Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Weather calculation program - TypeError: int()

Postby informatics109 » Wed Sep 14, 2016 7:56 pm

When I fixed it and ran the program, I recieved this error at line 14:

''temp.append(int(kolonne[8]))
ValueError: invalid literal for int() with base 10: 'TAM'''

How do we make the program start reading the values from the second line, not the first one?
informatics109
 
Posts: 3
Joined: Wed Sep 14, 2016 3:34 pm

Re: Weather calculation program - TypeError: int()

Postby Yoriz » Wed Sep 14, 2016 9:18 pm

You can use next to read a line from the file
change
Code: Select all
with open(filein,'r') as f:
    for line in f:
to
Code: Select all
with open(filein,'r') as f:
    next(f)
    for line in f:


note:
Code: Select all
with open(filein,'r') as f:

is all that is needed to open the file, the following lines are not needed
Code: Select all
infile = open(filein,'r')
f = infile.readlines()
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Yoriz
 
Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Weather calculation program - TypeError: int()

Postby informatics109 » Sat Sep 17, 2016 3:43 pm

Thanks man, for the help, much appreciated! :)
informatics109
 
Posts: 3
Joined: Wed Sep 14, 2016 3:34 pm


Return to Homework

Who is online

Users browsing this forum: No registered users and 4 guests

cron