Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is my for loop failing?
#1
Can you tell me why my for loop isn't working?

I'm working on managing my input from my game-pad controller device.

I store most of the details in a list.
I was trying to reset/clear the button array section of the list that holds the button states.
I thought I could use "button_state =0" in a for loop, but its not working the way I hoped.

C_LIST_BUTTONS_ARRAY =4

#   This list represents data obtained from game-pad.
controller =[0, 0, 0, 0, [1, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0]]

print("Initial state of array.")
for button_index in range(len(controller[C_LIST_BUTTONS_ARRAY])):
    button_state =controller[C_LIST_BUTTONS_ARRAY][button_index]
    print(button_state, end ="")
# end for
print()
# ---------------------------------------------------------------------------


print("\n\nResults of for loop using in object")

#   Reset all the button states in the button array.
for button_state in controller[C_LIST_BUTTONS_ARRAY]:
    button_state =0
# end for
#   _.

for button_state in controller[C_LIST_BUTTONS_ARRAY]:
    print(button_state, end ="")
# end for
print()
# ---------------------------------------------------------------------------


print("\n\nResults of for loop using range.")

#   Reset all the button states in the button array.
for button_index in range(len(controller[C_LIST_BUTTONS_ARRAY])):
    controller[C_LIST_BUTTONS_ARRAY][button_index] =0
# end for
#   _.


for button_state in controller[C_LIST_BUTTONS_ARRAY]:
    print(button_state, end ="")
# end for
print()
Output:
Initial state of array. 10000000000 Results of for loop using in object 10000000000 Results of for loop using range. 00000000000
Reply
#2
button_state point to non ref value. List, dict, and classes are ref value.
C_LIST_BUTTONS_ARRAY = 4
controller =[0, 0, 0, 0, [1, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0]]

print(controller[C_LIST_BUTTONS_ARRAY])
controller[C_LIST_BUTTONS_ARRAY] = [0 for i in range(len(controller[C_LIST_BUTTONS_ARRAY]))]
print(controller[C_LIST_BUTTONS_ARRAY])

When iterating over a list. It return list position value address. Not controller[4][0].
Python only create same value once in memory. All variables just point to objects memory.

C_LIST_BUTTONS_ARRAY = 4
controller =[0, 0, 0, 0, [1, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0]]

# list object address
print(id(controller[C_LIST_BUTTONS_ARRAY]))
for button_state in controller[C_LIST_BUTTONS_ARRAY]:
    print(id(button_state)) # value address
99 percent of computer problems exists between chair and keyboard.
Reply
#3
I'm not sure what you mean. Now you got me thinking I don't know line below is doing? Huh

button_state =0
Reply
#4
button_state = 0 is just assigning local value
class A:
    def __init__(self):
        self.a = 1

    def __repr__(self):
        return 'A:' + str(self.__dict__)

a = [0, A(), [1]]

for item in a:
    print(type(item))
    if isinstance(item, A):
        # changing class value
        item.a = 2
    elif isinstance(item, list):
        # changing list value
        item[0] = 5
    else:
        # assigning local value
        item = 1

print(a)
99 percent of computer problems exists between chair and keyboard.
Reply
#5
That's a good example,

Thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Failing to connect by 'net use' tester_V 1 194 Apr-20-2024, 06:31 AM
Last Post: tester_V
  Failing regex tester_V 3 1,196 Aug-16-2022, 03:53 PM
Last Post: deanhystad
  Failing to connect to a host with WMI tester_V 6 4,430 Aug-10-2021, 06:25 PM
Last Post: tester_V
  Failing to get Stat for a Directory tester_V 11 3,588 Jul-20-2021, 10:59 PM
Last Post: Larz60+
  Failing to Zip files tester_V 4 2,177 Dec-01-2020, 07:28 AM
Last Post: tester_V
  Python 3.8 on mac failing to start sgandon 0 2,923 Jan-14-2020, 10:58 AM
Last Post: sgandon
  trying to install oandapy and failing ErnestTBass 0 1,916 Feb-24-2019, 06:13 PM
Last Post: ErnestTBass
  Pyinstaller failing JP_ROMANO 2 4,086 Jan-16-2019, 06:07 PM
Last Post: JP_ROMANO

Forum Jump:

User Panel Messages

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