#πŸ”’ how do i change this into a function that changes the weight per year?

39 messages Β· Page 1 of 1 (latest)

orchid warren
#
weight = 16.5
moon_weight = 0.165
total_weight = 0
total_weight2 = 15

def Add(weight, moon_weight):
   total_weight = weight + moon_weight
   total_weight2=total_weight2+total_weight
   return total_weight


for year in range(1, 15):
   print(Add(weight, moon_weight))```
cedar brookBOT
#

@orchid warren

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

orchid warren
#

did some editing and got rid of an error, but now it won't print anything, why is that?

#
weight = 16.5
moon_weight = 0.165
total_weight = 0
total_weight2 = 15
for year in range(1, 15):
   def Add(weight, moon_weight):
      total_weight = weight + moon_weight
      total_weight2=total_weight2+total_weight
      return total_weight
      return total_weight2
      print(Add(weight, moon_weight))

#for year in range(1, 15):
   #print(Add(weight, moon_weight))```
slate crown
orchid warren
slate crown
#

I think you intended that last print to be outside of the function, but the function shouldn't be in the loop at all.

orchid warren
#

alright

slate crown
#

What's the error?

orchid warren
#

"UnoundLocalError: cannot access local variable 'total_weight2' where it is not associated with a value"

#

but i have it attached to the 15 above

slate crown
#

You need global total_weight2 at the top of the function. Because you'

#

're reassigning total_weight2, Python will treat it as a local by default. But if it's a local, there is no total_weight2 that exists before that line runs.

orchid warren
#

so i need to put global at the front?

slate crown
#

Just put global total_weight2 at the top of the function, on its own line.

#

You shouldn't be using globals, but since you are using globals and reassigning them, that global statement is necessary.

orchid warren
#

what should i use instead

slate crown
#

return. I'll note though that you can't have two returns like you have. As soon as return is reached, the function exits, so the return total_weight2 will never run.

orchid warren
#

so i don't need to call both then?

slate crown
#

!e

def func():
    print(1)
    return
    print(2)
func()
cedar brookBOT
#

@slate crown :white_check_mark: Your 3.12 eval job has completed with return code 0.

1
orchid warren
#

oh

slate crown
#

If you want to return two values at once, wrap them in a tuple: return total_weight, total_weight2.

#

!e

def func():
    return 1, 2
x, y = func()
print(x, y)
cedar brookBOT
#

@slate crown :white_check_mark: Your 3.12 eval job has completed with return code 0.

1 2
orchid warren
#

i'm rusty with tuples lol

slate crown
#

They're just immutable lists essentially.

#

If you understand lists, you basically understand tuples.

orchid warren
#

ok

#

brb

orchid warren
#

Back

slate crown
#

Ok

orchid warren
#

Kinda worried I’ll still be confused about this later

slate crown
#

And note sure. I only used VSCode briefly when it was first released.

orchid warren
#

ok

cedar brookBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.