Question:
Python programming problem, how to write this function?
Jack
2016-12-09 23:28:34 UTC
My code looks like this:

def readData():
infile=open("carModelData_city", 'r')
infile1=open("carModelData_highway", 'r')
stats1=infile.read().split()
stats=infile.read().split()
infile.close()




def averageMPG():
totals=sum(stats)
length=len(stats)
avg1=totals/length
print("The averge mpg city is", avg1)
totals1=sum(stats1)
length1=len(stats1)
avg2=totals/length
print("The average mpg highway is", avg2)
average=(avg1+avg2)/2
print("The combined averge mpg is", average)



def main():
readData()
averageMPG()
main()
My syntax errors look like this:
Traceback (most recent call last):
File "C:\Users\Jobin\Documents\csc\assn4\att1.py", line 36, in
main()
File "C:\Users\Jobin\Documents\csc\assn4\att1.py", line 32, in main
averageMPG()
File "C:\Users\Jobin\Documents\csc\assn4\att1.py", line 17, in averageMPG
totals=sum(stats)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Any help would be greatly appreciated!
Three answers:
husoski
2016-12-10 00:23:25 UTC
I don't see how your code got that far. Your readData() function is reading into local variables (stats, stats1), which get deleted as soon as the function returns. (Also, were you supposed to read stats1 from infile1? Right now you're reading both from infile.)



The TypeError exceptions seems to be happening because you are trying to use sum() on a list of strings. You don't convert the results of split() into floats anywhere. More about that below.



One way to solve the local variable problem is to use global variables. That's not the best habit to get into, since globals tend to make "brittle" programs that are hard to modify and "refactor" as they grow and as requirement evolve. In each function that uses them (including main), add the statement:



global stats, stats1



...before any use of either of them.



I think it's better for the input function to return those lists to main() and for main to pass them as arguments to other functions. One nice way to do this (if the assignment allows for it) is to pass the file name to the readData() function, with something like:



def readData(fname):

.... infile = open(fname)

.... stats = []

.... for s in infile.read().split():

.... .... stats.append(float(s))

.... return stats



That will read a specified file and convert all string values to floats. The resulting list is returned as a function result. Then, main() can:



.... stats_hwy = readData( "carModelData_hwy" )

.... stats_city = read_data( "carModelData_city" )



and the pass those lists to avgData() with:



.... avgData(stats_city, stats_hwy)



Update the header of avgData() to:



def avgData(stats, stats1):



...using the old names if you like. I'd change stats to stats_city and stats1 to stats_hwy, so that the variable name says what the variable contains. You might prefer statsCity and statsHwy to match the camelCase naming style that you're using for function names. I tend to follow the styles that the Python libraries use.



Notice how the new version of readData() means you only write (or change!) the file input code once, instead of twice to get both files read. The only differences were the file names and the names of the resulting list of values, and that's handled nicely by arguments and return values.
?
2016-12-10 02:28:51 UTC
I don't know the exact answer but it appears you're trying to do math with a string and an integer.
brilliant_moves
2016-12-10 00:02:44 UTC
Instead of this:

print("The averge mpg city is", avg1)

you need this:

print("The averge mpg city is", str(avg1))



Then do the same with your other two print statements.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...