Python Forum
Looping through nested elements and updating the original list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping through nested elements and updating the original list
#1
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)
Output:
Add points to Strength: 2 Strength: 2 Points: 28 Add points to Endurance: 3 Endurance: 3 Points: 25 Add points to Wisdom: 4 Wisdom: 4 Points: 21 Add points to Dexterity: 5 Dexterity: 5 Points: 16 [['Strength:', 0], ['Endurance:', 0], ['Wisdom:', 0], ['Dexterity:', 0]] 16
Yoriz write Aug-18-2021, 09:24 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
If you change line 11
score += add_points
to
element[1] += add_points
it will actually alter the original list
Reply
#3
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]?
Reply
#4
Thanks very much for your help and for the guidance on the correct formatting of posts! Yes, using a dictonary does make more sense
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python 3.8 Nested varible not updating Teknohead23 6 590 Oct-02-2021, 11:49 AM
Last Post: Teknohead23
  Why am I getting list elements < 0 ? Mark17 8 810 Aug-26-2021, 09:31 AM
Last Post: naughtyCat
  Why changing data in a copied list changes the original list? plumberpy 3 636 Aug-14-2021, 02:26 AM
Last Post: plumberpy
  Extracting Elements From A Website List knight2000 2 575 Jul-20-2021, 10:38 AM
Last Post: knight2000
  Make Groups with the List Elements quest 2 622 Jul-11-2021, 09:58 AM
Last Post: perfringo
  I cannot delete and the elements from the list quest 4 1,088 May-11-2021, 12:01 PM
Last Post: perfringo
  List of lists - merge sublists with common elements medatib531 1 1,204 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Find Common Elements in 2 list quest 4 1,019 Apr-14-2021, 03:57 PM
Last Post: quest
  Sorting Elements via parameters pointing to those elements. rpalmer 3 1,004 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  Group List Elements according to the Input with the order of binary combination quest_ 19 2,237 Jan-28-2021, 03:36 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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