Aug-18-2021, 09:00 PM
(This post was last modified: Aug-18-2021, 09:24 PM by Yoriz.
Edit Reason: Added code tags
)
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