Python Forum
cannot reset list in for loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
cannot reset list in for loop
#1
I am trying to write a short program to solve a measurement puzzle with three jiggers of water. I want it to loop through a list of tuples, each one of which describes a pouring jigger and a receiving jigger by its size and its content. After running through the for loop once in which a function modifies the jigger contents, I want to be able to reset the jigger to its original contents and run through the for loop a second time. I do not seem to be able to reset the contents of the jigger to its original state for the second run through the for loop.
# oneLiterPourSolver_test.py
# figures out a classic 3 jigger pour puzzle

jigger3 = [3,2] 
jigger5 = [5,3] 
jigger8 = [8,5] 
pour_1 = [(jigger3,jigger5),(jigger3, jigger8)] 


def resetJiggers():
    global jigger3, jigger5, jigger8, pour_1
    jigger3 = [3,2] 
    jigger5 = [5,3] 
    jigger8 = [8,5]
    #         [(pour from 3 into 5),(pour from 3 into 8)]
    pour_1 = [(jigger3,jigger5),(jigger3, jigger8)] 
    
def firstPour(fromJigger, toJigger):
        toJigger[1] = toJigger[1] + fromJigger[1]
        fromJigger[1] = 0
        
    
for (x,y) in pour_1:
    firstPour(x,y)
    resetJiggers()
I know I'm missing something very simple. I've tried using globals (everywhere!) AND moving the call to resetJiggers() to different position, all to no avail. My debugger (in IDLE) indicates that, once called, the for loop uses the changed values of jigger3 and jigger5 (instead of the reset values) no matter what...
Reply
#2
Scope.
Rule 1: Never use globals
Rule 2: Never use globals
Rule 3: you get the idea

# oneLiterPourSolver_test.py
# figures out a classic 3 jigger pour puzzle
 
jigger3 = [3,2] 
jigger5 = [5,3] 
jigger8 = [8,5] 
pour_1 = [(jigger3,jigger5),(jigger3, jigger8)] 
 
def resetJiggers():

    j3 = [3,2] 
    j5 = [5,3] 
    j8 = [8,5]
    #         [(pour from 3 into 5),(pour from 3 into 8)]
    p1 = [(j3,j5),(j3, j8)] 
    return j3, j5, j8, p1
     
def firstPour(fromJigger, toJigger):
        toJigger[1] = toJigger[1] + fromJigger[1]
        fromJigger[1] = 0
         
     
for (x,y) in pour_1:
    firstPour(x,y)
    jigger3, jigger5, jigger8, pour_1 = resetJiggers()

print(jigger3, jigger5, jigger8, pour_1)
Output:
[3, 2] [5, 3] [8, 5] [([3, 2], [5, 3]), ([3, 2], [8, 5])]
Reply
#3
Thanks, Minister Jeff... I've recast the problem, with your help, and I'll post the solution when I've uncovered it. Do you know of a good discussion on scope in functions? I need to go back and relearn...
Reply
#4
This isn't bad. https://www.youtube.com/watch?v=QVdf0LgmICw
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,130 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
Question Reset list if user regrets Gilush 1 2,073 Dec-05-2020, 10:55 AM
Last Post: Gilush
  Appending to list of list in For loop nico_mnbl 2 2,369 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl
  Append list into list within a for loop rama27 2 2,385 Jul-21-2020, 04:49 AM
Last Post: deanhystad
  loop through list or double loop 3Pinter 4 3,446 Dec-05-2018, 06:17 AM
Last Post: 3Pinter
  Write a for loop on list of lists without changing the shape of the main list Antonio 3 3,759 Jun-19-2018, 02:16 AM
Last Post: ichabod801
  For looping over a list, editing the list from inside the loop? Krookroo 3 3,948 Sep-04-2017, 05:08 PM
Last Post: Krookroo
  How to change from printFacts ( ) to return a list & Loop over list when writing CSV Ivan1 14 8,312 Aug-30-2017, 12:14 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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