Python Forum
Can a function modify a variable outside it?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can a function modify a variable outside it?
#1
I can get a for loop or while loop to modify a variable outside them, but I can't figure out how to get a function to do that.

# THIS WORKS
list_len = 0

while list_len < 100:
    list_len += 1

print(list_len)
# THIS DOES NOT WORK
list_len = 0

def list_inc():
    list_len += 1

list_inc()

# UnboundLocalError: local variable 'list_len' referenced before assignment
Reply
#2
(Jul-29-2019, 11:48 PM)Exsul Wrote: I can get a for loop or while loop to modify a variable outside them, but I can't figure out how to get a function to do that.

# THIS WORKS
list_len = 0

while list_len < 100:
    list_len += 1

print(list_len)
# THIS DOES NOT WORK
list_len = 0

def list_inc():
    list_len += 1

list_inc()

# UnboundLocalError: local variable 'list_len' referenced before assignment

Maybe you will read about global variables note that is bad, I will suggest you not use global variables, you can do it like

list_len = 0

def list_inc(list_):
    return list_ +1

list_len = list_inc(list_len)
Reply
#3
I'm very new to programming. Why are global variables bad, and what should I use instead?
Reply
#4
(Jul-30-2019, 12:11 AM)Exsul Wrote: I'm very new to programming. Why are global variables bad, and what should I use instead?

Read about global variables http://wiki.c2.com/?GlobalVariablesAreBad
This is your code using global variable *workable

# THIS NOW WORK SEE THE LINE 2

def list_inc():
    global list_len
    list_len += 1
 
list_inc()
To avoid using local variable you can use

list_len = 0
 
def list_inc(list_):
    return list_ +1
 
list_len = list_inc(list_len)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable for the value element in the index function?? Learner1 8 633 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 576 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,281 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 924 Aug-07-2023, 05:58 PM
Last Post: Karp
  Retrieve variable from function labgoggles 2 1,038 Jul-01-2022, 07:23 PM
Last Post: labgoggles
  Cant transfer a variable onto another function KEIKAS 5 1,880 Feb-09-2022, 10:17 PM
Last Post: deanhystad
  I am writing a car rental program on python. In this function I am trying to modify aboo 2 2,730 Aug-05-2021, 06:00 PM
Last Post: deanhystad
  how to modify a variable that is passed as parameter StefanL38 2 2,111 Dec-07-2020, 08:39 AM
Last Post: StefanL38
  Please explain uncommon way of declaring and using variable [function.variable] esphi 4 2,324 Nov-07-2020, 08:59 AM
Last Post: buran
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 2,206 Sep-18-2020, 02:50 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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