2

I'm trying to make a function that creates a dictionary of "player stats", based off of inputs from the user. However I'm running into several issues with my code and I know there are better ways of programming this but I can't figure out what they are.

def playerStats(): standardArray = [15, 14, 13, 12, 10, 8] stats = {"Strength":[], "Dexterity":[], "Constitution":[], "Intelligence":[], "Wisdom":[], "Charisma":[]} while standardArray: print("Please select a value from this list for each one of your character stats!") print(standardArray) statSelect1 = input("Please select how many points you would like your 'Strength' stat to start with: ") if statSelect1 in standardArray: print(f"You have selected {statSelect1} as your base Strength!") stats["Strength"].append(statSelect1) standardArray.pop(0) print(standardArray) statSelect2 = input("Please select how many points you would like your 'Dexterity' stat to start with: ") if statSelect2 in standardArray: print(f"You have selected {statSelect1} as your base Strength!") stats["Dexterity"].append(statSelect2) standardArray.pop(0) print(standardArray) statSelect3 = input("Please select how many points you would like your 'Constitution' stat to start with: ") if statSelect3 in standardArray: print(f"You have selected {statSelect1} as your base Strength!") stats["Constitution"].append(statSelect3) standardArray.pop(0) print(standardArray) statSelect4 = input("Please select how many points you would like your 'Intelligence' stat to start with: ") if statSelect4 in standardArray: print(f"You have selected {statSelect1} as your base Strength!") stats["Intelligence"].append(statSelect4) standardArray.pop(0) print(standardArray) statSelect5 = input("Please select how many points you would like your 'Wisdom' stat to start with: ") if statSelect5 in standardArray: print(f"You have selected {statSelect1} as your base Strength!") stats["Wisdom"].append(statSelect5) standardArray.pop(0) print(standardArray) statSelect6 = input("Please select how many points you would like your 'Charisma' stat to start with: ") if statSelect6 in standardArray: print(f"You have selected {statSelect1} as your base Strength!") stats["Charisma"].append(statSelect6) standardArray.pop(0) print("Your character stats are:") print(stats) playerStats() 

I think much of the issue is from the way I'm trying to place the stats into my dictionary. However what I have mostly works for what I need, but the first issue is if for example I set my strength to 12 points, the 15 will pop from the list and I'm not sure how to make the 12 pop instead. Also, for some reason "print(f"You have selected {statSelect1} as your base Strength!")" is not printing.

4
  • 1
    Why are you doing stats[...].append()? There's no reason to use a list. Just do stas[...] = ... to assign it directly. Beyond that, I suggest creating a Stats or Character class instead of using a dictionary. Commented Apr 16, 2022 at 4:29
  • Does this answer your question? Best way to remove elements from a list Commented Apr 16, 2022 at 4:31
  • Also: stackoverflow.com/questions/627435/… Commented Apr 16, 2022 at 4:32
  • In your example, you would need to find 12 in the list i.e. x = standardArray.index(12) and the pop that index (3) i.e. standardArray.pop(x) but be careful of duplicate values. Commented Apr 16, 2022 at 4:33

2 Answers 2

1

Use remove to remove an element by value.

Also use a loop to iterate over the stats:

standardArray = [15, 14, 13, 12, 10, 8] stats = {"Strength":[], "Dexterity":[], "Constitution":[], "Intelligence":[], "Wisdom":[], "Charisma":[]} print("Please select a value from this list for each one of your character stats!") for s in stats: choice = None while True: choice = input(f"Please select how many points you would like your '{s}' stat to start with: \nvalid entries are {standardArray} :") try: choice = int(choice) except ValueError: print(f'"{choice}" is not a valid input') continue if choice in standardArray: stats[s].append(choice) standardArray.remove(choice) break print(stats) 
Sign up to request clarification or add additional context in comments.

Comments

1

Get the index of the value via the index method and pop that instead of 0.

Also, why have the while loop? It does nothing.

1 Comment

It might be helpful to provide code or an example of using that method within the context

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.