Python Forum
function error: undefined variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function error: undefined variable
#1
Hi,
This is my first def function and i cannot figure out why i get a, Nameerror: name 'mpg' is not defined. Any help is much appreciated!!. Smile

#!/usr/bin/env python3
def calculateMPG(milesDriven,gallonsUsed):
    mpg=milesDriven/gallonsUsed
    mpg=round(mpg,1)
    return mpg

def main():
    choice='y'
    while choice.lower()=='y':
        #get input form user
        milesDriven=float(input('enter miles driven'))
        gallonsUsed=float(input('enter gallons used'))
        #call MPG function
        calculateMPG(milesDriven,gallonsUsed)
        print('miles per gallon:\t',mpg)
        #determine fate of loop
        choice=input('do you want to continue: y/n')
Reply
#2
mpg is assigned on line 2, so it's in scope for calculateMPG(). But I don't see that it's assigned anywhere in main(). To be used there, it needs to be assigned in that function (a local), or in an enclosing scope (such as a global). As it's not found there, it's undefined when you reach line 15.
Reply
#3
Ah yes!
#!/usr/bin/env python3
def calculateMPG(milesDriven,gallonsUsed):
    mpg=milesDriven/gallonsUsed
    mpg=round(mpg,1)
    return mpg

def main():
    choice='y'
    while choice.lower()=='y':
        #get input form user
        milesDriven=float(input('enter miles driven'))
        gallonsUsed=float(input('enter gallons used'))
        mpg=calculateMPG(milesDriven,gallonsUsed)
        print(mpg)
        #determine fate of loop
        choice=input('do you want to continue: y/n')

Thank you very much. But can you tell me why it didn't work when I tried to set mpg=0 local within the def main().
Reply
#4
Depends on what else was happening. It should have removed the specific error, but mpg would have just been zero. You'd need to post the code and tell what you expected to happen and how that differed from what actually did happen.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable for the value element in the index function?? Learner1 8 629 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 571 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,276 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 921 Aug-07-2023, 05:58 PM
Last Post: Karp
  undefined function error JonWayn 5 1,440 Sep-11-2022, 03:38 AM
Last Post: JonWayn
  Undefined Led_Zeppelin 4 1,409 Aug-02-2022, 11:57 AM
Last Post: buran
  Retrieve variable from function labgoggles 2 1,037 Jul-01-2022, 07:23 PM
Last Post: labgoggles
  [variable] is not defined error arises despite variable being defined TheTypicalDoge 4 2,123 Apr-05-2022, 04:55 AM
Last Post: deanhystad
  Cant transfer a variable onto another function KEIKAS 5 1,879 Feb-09-2022, 10:17 PM
Last Post: deanhystad
  ImportError: /home/pybind11_example.cpython-37m-x86_64-linux-gnu.so: undefined symbol chaitra 2 5,095 Feb-03-2021, 05:14 AM
Last Post: chaitra

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020