0

*I'm a Beginner... My friend tried to help me a bit with this but I can't seem to solve it. I'm not really sure what to do so any help would be greatly appreciated.I get the following error in averageMPG,"name stats city is parameter and global".I also wasn't really sure how to write both functions in my readData function as you can see. The problem is in the picture. I haven't succeeded in part b so I haven't moved on,

def readData(carmodelData_city):

    global stats_city,stats_hwy
    infile=open("carModelData_city", 'r')
    stats_city=[]
    for s in infile.read.split():
        stats.append(float(s))
        return stats_city


def read_Data(carmodelData_hwy):
    global stats_city,stats_hwy
    infile=open("carModelData_hwy", 'r')
    stats_hwy=[]
    for s in infile.read.split():
        stats.append(float(s))
        return stats_hwy        


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


def main():
global stats_city,stats_hwy
stats_city=readData("carModelData_city", "r")
stats_hwy=read_Data("carModelData_hwy", "r")



[enter image description here][1]main()
3
  • You've missed the picture. Also you probably shouldn't use a picture if you can use text. Commented Dec 10, 2016 at 1:45
  • 1
    Welcome to SO. Generally when you post code, it needs to be a minimum working example, otherwise users can't run your code. In this case it turns out the issue is obvious (see the answer by ShadowRanger), but in general you can't have your code depend on a data file and expect people to just be able to figure out what's wrong. Commented Dec 10, 2016 at 1:46
  • @ssdecontrol I apologize. This is my first time posting, and I wasn't too sure how to format things. This site is a lot more thorough and informative than others. I'll be sure to include the necessary files, and work on formatting. Thanks for the comment. Commented Dec 10, 2016 at 1:58

2 Answers 2

1

You named a function parameter stats_city, and also declared it a global value. Those two things are incompatible.

AFAICT, none of your code actually requires anything to be global in the first place, so stop declaring everything global, and you should be fine.

Well, fine on that specific error anyway. The massive overuse of global here feels an awful lot like cargo cult programming, and you have many other problems (e.g. infile.read.split() is going to try to split the read method of the file; you forgot parens, so it's not actually calling read to get data back). You're also returning at the end of the first iteration of each loop, when I suspect you want to finish the loops and return the accumulated values. You need to learn a lot more of the basics here; please talk to a professor or a tutor.

Sign up to request clarification or add additional context in comments.

Comments

0

Also, your "stats.append()" calls should probably be "stats_city.append" in the first function and "stats_hwy.append" in the second. You will return after one iteration in each function unless you adjust your idents on the return call.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.