What can I do to stop getting some 'local unbound' error message? [duplicate]












-2
















This question already has an answer here:




  • Python variable scope error

    11 answers



  • Local (?) variable referenced before assignment [duplicate]

    3 answers




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]










share|improve this question















marked as duplicate by Klaus D., Brad Solomon, Li357, Michael Butscher, Makoto Nov 23 '18 at 4:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • That code does not contain any variable named numberOfTripsCompleted. Please post the actual code.

    – John Gordon
    Nov 23 '18 at 3:32
















-2
















This question already has an answer here:




  • Python variable scope error

    11 answers



  • Local (?) variable referenced before assignment [duplicate]

    3 answers




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]










share|improve this question















marked as duplicate by Klaus D., Brad Solomon, Li357, Michael Butscher, Makoto Nov 23 '18 at 4:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • That code does not contain any variable named numberOfTripsCompleted. Please post the actual code.

    – John Gordon
    Nov 23 '18 at 3:32














-2












-2








-2









This question already has an answer here:




  • Python variable scope error

    11 answers



  • Local (?) variable referenced before assignment [duplicate]

    3 answers




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]










share|improve this question

















This question already has an answer here:




  • Python variable scope error

    11 answers



  • Local (?) variable referenced before assignment [duplicate]

    3 answers




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]





This question already has an answer here:




  • Python variable scope error

    11 answers



  • Local (?) variable referenced before assignment [duplicate]

    3 answers








python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 20:58







Temie

















asked Nov 23 '18 at 3:07









TemieTemie

11




11




marked as duplicate by Klaus D., Brad Solomon, Li357, Michael Butscher, Makoto Nov 23 '18 at 4:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Klaus D., Brad Solomon, Li357, Michael Butscher, Makoto Nov 23 '18 at 4:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • That code does not contain any variable named numberOfTripsCompleted. Please post the actual code.

    – John Gordon
    Nov 23 '18 at 3:32



















  • That code does not contain any variable named numberOfTripsCompleted. Please post the actual code.

    – John Gordon
    Nov 23 '18 at 3:32

















That code does not contain any variable named numberOfTripsCompleted. Please post the actual code.

– John Gordon
Nov 23 '18 at 3:32





That code does not contain any variable named numberOfTripsCompleted. Please post the actual code.

– John Gordon
Nov 23 '18 at 3:32












1 Answer
1






active

oldest

votes


















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






share|improve this answer


























  • 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 4: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


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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






share|improve this answer


























  • 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 4: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
















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






share|improve this answer


























  • 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 4: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














0












0








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






share|improve this answer















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







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 4:36

























answered Nov 23 '18 at 3:42









HayatHayat

601618




601618













  • 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 4: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



















  • 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 4: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

















i get a typeerror when i try this. thanks though

– Temie
Nov 24 '18 at 20:49





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 4:37





That runs perfectly fine for me. Though I have made some changes which will help you understand the flow.

– Hayat
Nov 25 '18 at 4: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





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





這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini