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.
Advertisement
Answer
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)