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


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 477 Jan-27-2024, 04:03 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 505 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  for loops break when I call the list I'm looping through Radical 4 922 Sep-18-2023, 07:52 AM
Last Post: buran
  List all possibilities of a nested-list by flattened lists sparkt 1 940 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Checking if a string contains all or any elements of a list k1llcod3 1 1,131 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  How to change the datatype of list elements? mHosseinDS86 9 2,028 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,193 May-17-2022, 11:38 AM
Last Post: Larz60+
  Updating nested dict list keys tbaror 2 1,299 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Python Program to Find the Total Sum of a Nested List vlearner 8 5,005 Jan-23-2022, 07:20 PM
Last Post: menator01
  Python 3.8 Nested varible not updating Teknohead23 6 2,471 Oct-02-2021, 11:49 AM
Last Post: Teknohead23

Forum Jump:

User Panel Messages

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