Determining syntax or logical error
List/Function Logic Error
Help Request: Logic Error in defined functions.
I am not getting any printed output from the step() function I defined. My program did not end
You are getting a lot of "negative vibes" because we see errors in your function that will crash it. Yet you are saying the code did not crash. So we suspect that you haven't posted the actual code you are running.
Just talking about your step() function. This bit of simplified code shows your problem. Leave the # line as-is and run it.
btls = 3000
def step():
# global btls # after first execution remove the #
btls -= 1
print(f"before: {btls=}")
step()
print(f" after: {btls=}")
With the global commented out, this prints:
before: btls=3000
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
start(fakepyfile,mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), __main__.__dict__)
File "<string>", line 8, in <module>
File "<string>", line 5, in step
UnboundLocalError: cannot access local variable 'btls' where it is not associated with a value
So your code will definitely stop when you call the step() function. And when you call the pickUpBottle() function, and the putDownBottles() function. All for the same reason: if you assign to a name inside a function that name is considered local to the function unless the name is made global in the function. Now delete the # character from the sample global line and run it again. Now it's doing what you want.
In addition, you haven't formatted your code correctly, which means we can't run it. The moveBottlesOverWeightLimit() function has an indentation problem. When quoting code here you should be copy/pasting code into reddit. Fixing it up by hand is a waste of time.