#π how do i change this into a function that changes the weight per year?
39 messages Β· Page 1 of 1 (latest)
@orchid warren
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.
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))```
Because you never call the function. Why is def Add inside the loop?
part of experimenting, i get an error if it's outside
I think you intended that last print to be outside of the function, but the function shouldn't be in the loop at all.
alright
What's the error?
"UnoundLocalError: cannot access local variable 'total_weight2' where it is not associated with a value"
but i have it attached to the 15 above
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.
so i need to put global at the front?
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.
what should i use instead
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.
so i don't need to call both then?
!e
def func():
print(1)
return
print(2)
func()
@slate crown :white_check_mark: Your 3.12 eval job has completed with return code 0.
1
oh
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)
@slate crown :white_check_mark: Your 3.12 eval job has completed with return code 0.
1 2
i'm rusty with tuples lol
They're just immutable lists essentially.
If you understand lists, you basically understand tuples.
Back
Ok
Btw do you know how to change the layout in visual studio?
Kinda worried Iβll still be confused about this later
I'm assuming you mean VSCode?
And note sure. I only used VSCode briefly when it was first released.
ok
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.