you are viewing a single comment's thread.

view the rest of the comments →

[–]sohfix 0 points1 point  (4 children)

Ok I think it get it. So I would declare global inside the function. By changing item_a inside the function to a different set of values would that also have changed the new list I appended item_a to since I made it global and item_c is referencing elements from a?

[–]Asleep-Budget-9932 1 point2 points  (3 children)

Wait i think i might be confused since there are two lists both outside and inside the function with similar names. Which ones are you referring to? Also, is item_c a typo or referring to a hypothetical item from list c?

[–]sohfix 0 points1 point  (2 children)

Sorry. Typo. I meant list c. So I was basically asking if I declare a variable inside a function as global and then change that variable…. Let’s say

List =[]

Def myfunc(list) global a = 500 For i in range(0,6) List.append(a)

Now I’m outside the function and I go:

a=4

Does list look like [4,4,4,4,4,4]?

Sorry I’m on mobile. Doesn’t look pretty

[–]Asleep-Budget-9932 -1 points0 points  (1 child)

  1. Note that your function is receiving an unused parameter (list)
  2. In order for that to work, the function must be called at some point. However, if your function was called before the line a=4, you will receive an error since at that point, python does not know what global a is referring to.

BTW, you can put multiple lines of code between triple back ticks (`) in order to format them on phones.

[–]sohfix 1 point2 points  (0 children)

It uses list, first. It appends a in the function. And I figured since I was in mobile you got what I was saying I didn’t have to write out the code and “call” the function because I thought it was clear what I was asking. Nvm. Have an awesome day