![]() |
Looping through nested elements and updating the original list - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Looping through nested elements and updating the original list (/thread-34663.html) |
Looping through nested elements and updating the original list - Alex_James - Aug-18-2021 I am trying to create a code that lets the user add points to attributes. I have created the code below which loops through each element. It all works fine apart from the original list 'attributes' not updating once the user has gone through the loop. This means the points aren't actually added. How do you ensure the points are added to the original list? My code and the output are shown below. points = 30 attributes = [["Strength:", 0],["Endurance:", 0],["Wisdom:", 0],["Dexterity:", 0]] for element in attributes: attribute, score = element print("\nAdd points to", attribute) add_points = int(input("")) if points - add_points < 0: print("Error. You do not have enough points.") else: score += add_points points -= add_points print(attribute, score) print("Points:",points) print(attributes) print(points)
RE: Looping through nested elements and updating the original list - Yoriz - Aug-18-2021 If you change line 11 score += add_pointsto element[1] += add_pointsit will actually alter the original list RE: Looping through nested elements and updating the original list - deanhystad - Aug-18-2021 Please wrap code in Python tags so it retains indenting and wrap output in output tags so it isn't confused for regular text in the post. There are buttons in the editor. You need to modify values in attributes, or in elements. Modifying the value of score does nothing to your attributes list. points = 30 attributes = [["Strength:", 0],["Endurance:", 0],["Wisdom:", 0],["Dexterity:", 0]] for element in attributes: print("\nAdd points to", element[0]) add_points = int(input("")) if points - add_points < 0: print("Error. You do not have enough points.") else: element[1] += add_points # <= Need to change value in element points -= add_points print(element) print(attributes) print(points)You should not be using a list of lists for attributes. attributes is a dictionary mapping names to scores, you should make it a Python dictionary. points = 30 attributes = {"Strength":0, "Endurance":0, "Wisdom":0, "Dexterity":0} for element in attributes: add_points = min(int(input(f"\nAdd points to {element}: ")), points) attributes[element] += add_points points -= add_points print(element, attributes[element]) if points <= 0: print('You are out of points') break print(attributes) print(points)Now that attributes is a dictionary you can use attribute['Strength"] to get the value of the Strength attribute. Isn't that better than attribute[0][1]? RE: Looping through nested elements and updating the original list - Alex_James - Aug-19-2021 Thanks very much for your help and for the guidance on the correct formatting of posts! Yes, using a dictonary does make more sense |