-2

I am getting an error saying "UnboundLocalError: local variable 'numberOfTripsCompleted' referenced before assignment"

trips= 0
def increasetrips(x):
    trips += 1

so I construct a list: ['driver', firstname, lastname,vehicletype, trips]. After instantiating this with an example: ['driver', Bob, Brown, truck, 0], I now want to have a function to increase the 'trips' part of the list by 1 each time. The function I have above does not give an error, but it after running ' increasetrips', when I call back for my instantiated list, position 4 still holds '0'. So it keeps returning ['driver', Bob, Brown, truck, 0], when i am expecting ['driver', Bob, Brown, truck, 1]

Temie
  • 1
  • 1

1 Answers1

0

Try this.

trips= 0
def increasetrips(trips):
     trips += 1
     return trips
trips = increasetrips(trips)

The parameter x you are passing inside the function. And then using trips which is defined outside the function

Hayat
  • 1,539
  • 4
  • 18
  • 32
  • i get a typeerror when i try this. thanks though – Temie Nov 24 '18 at 20:49
  • That runs perfectly fine for me. Though I have made some changes which will help you understand the flow. – Hayat Nov 25 '18 at 04:37
  • Thanks. It' s still not incrementing though :-( I have updated the question to make my problem clearer. You may check it out if you wish. Thanks – Temie Nov 25 '18 at 15:29