2

Whenever I run this code it tells me that on line 19 distanceTravelled is not defined, but I thought I defined it in the function. Thanks for any help.

startKilometre = float(input("What is the starting kilometre?"))
finalKilometre = float(input("What is the final kilomotre"))
finalFuel = float(input("How much fuel did you put in the car?"))

def functionTravelled(startKilometre, finalKilometre):
    distanceTravelled = finalKilometre - startKilometre
    return distanceTravelled



def functionLitresKilo(distanceTravelled, finalFuel):
    litresPerKilometre = distanceTravelled / finalFuel
    return litresPerKilometre

def functionKiloLitres(finalFuel, distanceTravelled):
    kilometresPerLitre = finalFuel / distanceTravelled
    return kilometresPerLitre

print(distanceTravelled)
print(litersPerKilometre)
print(kilometresPerLitre)
H. Seigne
  • 45
  • 4
  • You did define it in a function, but you did not execute that function. :) – 2Cubed May 29 '16 at 23:39
  • You might want to do some basic research, such as checking out the [relevant section of the official tutorial](https://docs.python.org/3.4/tutorial/controlflow.html#defining-functions). – TigerhawkT3 May 29 '16 at 23:45

4 Answers4

5

You are never calling the functions to get the values they are returned, thus, your code is running like:

startKilometre = float(input("What is the starting kilometre?"))
finalKilometre = float(input("What is the final kilomotre"))
finalFuel = float(input("How much fuel did you put in the car?"))

print(distanceTravelled)
print(litersPerKilometre)
print(kilometresPerLitre)

Which, of course, is not what you want. You need to call each function and store the return result inside a variable:

startKilometre = float(input("What is the starting kilometre?"))
finalKilometre = float(input("What is the final kilomotre"))
finalFuel = float(input("How much fuel did you put in the car?"))

def functionTravelled(startKilometre, finalKilometre):
    distanceTravelled = finalKilometre - startKilometre
    return distanceTravelled

def functionLitresKilo(distanceTravelled, finalFuel):
    litresPerKilometre = distanceTravelled / finalFuel
    return litresPerKilometre

def functionKiloLitres(finalFuel, distanceTravelled):
    kilometresPerLitre = finalFuel / distanceTravelled
    return kilometresPerLitre

distanceTravelled = functionTravelled(startKilometre, finalKilometre)
litersPerKilometre = functionLitresKilo(distanceTravelled, finalFuel)
kilometresPerLitre = functionKiloLitres(finalFuel, distanceTravelled)

print(distanceTravelled)
print(litersPerKilometre)
print(kilometresPerLitre)
Alexander Craggs
  • 7,874
  • 4
  • 24
  • 46
1

You're trying to print a non-existent variable. The variable is defined locally within your functionTravelled function. You want to print what these functions return (and you never actually call your functions).

e.g. -

startKilometre = float(input("What is the starting kilometre?"))
finalKilometre = float(input("What is the final kilomotre"))
finalFuel = float(input("How much fuel did you put in the car?"))

def functionTravelled(startKilometre, finalKilometre):
    distanceTravelled = finalKilometre - startKilometre
    return distanceTravelled

travelled = functionTravelled(startKilometre, finalKilometre)
print(travelled)

Similarly for your other print statements / functions.

Pythonista
  • 11,377
  • 2
  • 31
  • 50
1

Kinda like "What goes on in Vegas, stays in Vegas", you defined the variable in the function and it is not visible outside of the function. This is done on purpose so that you don't have to worry about using unique names for variables in functions.

Your functions are built to return the variable's value, so just call them. Here I assign variables in the global scope by calling the functions.

startKilometre = float(input("What is the starting kilometre?"))
finalKilometre = float(input("What is the final kilomotre"))
finalFuel = float(input("How much fuel did you put in the car?"))

def functionTravelled(startKilometre, finalKilometre):
    distanceTravelled = finalKilometre - startKilometre
    return distanceTravelled



def functionLitresKilo(distanceTravelled, finalFuel):
    litresPerKilometre = distanceTravelled / finalFuel
    return litresPerKilometre

def functionKiloLitres(finalFuel, distanceTravelled):
    kilometresPerLitre = finalFuel / distanceTravelled
    return kilometresPerLitre

distanceTravelled = functionTravelled(startKilometre, finalKilometre)
print(distanceTravelled)

etc....
tdelaney
  • 73,364
  • 6
  • 83
  • 116
0

It looks like you need to do a bit more research into how functions are defined and called. You are thinking of them as blocks of code that are indented for fun, and have no practical reason for being used. This is not the case. Functions are defined, and then called, at which point they return a value to the caller. The following is not a function:

a = 0
print(a)

Putting it into a function doesn't work the same way, as seen in this slimmed-down version of what you are doing:

def x():
    a = 0
    return a
print(a)

Functions must be called by their actual name, and you might want to save the value that gets returned:

def x():
    a = 0
    return a
var = x()
print(var)

You can learn more about functions from the official tutorial's section on defining functions, as well as many other places on the Internet if you run a Google search for how to define and use functions in Python.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • Sorry just starting learning python for school. I find it a bit difficult to remember all of the rules. – H. Seigne May 30 '16 at 02:22