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
#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


Messages In This Thread
RE: Looping through nested elements and updating the original list - by deanhystad - Aug-18-2021, 09:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  What is a faster way to deep copy a nested list without using .deepcopy()? Shervin_Ataeian 1 1,722 Oct-13-2024, 01:28 PM
Last Post: Pedroski55
  unable to remove all elements from list based on a condition sg_python 3 1,797 Jan-27-2024, 04:03 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 1,777 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  for loops break when I call the list I'm looping through Radical 4 2,071 Sep-18-2023, 07:52 AM
Last Post: buran
  List all possibilities of a nested-list by flattened lists sparkt 1 1,854 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Checking if a string contains all or any elements of a list k1llcod3 1 5,282 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  How to change the datatype of list elements? mHosseinDS86 9 3,923 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 8,541 May-17-2022, 11:38 AM
Last Post: Larz60+
  Updating nested dict list keys tbaror 2 2,016 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Python Program to Find the Total Sum of a Nested List vlearner 8 8,450 Jan-23-2022, 07:20 PM
Last Post: menator01

Forum Jump:

User Panel Messages

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