I am trying to iterate through a loop assigning values to stats from user input. I essentially want to use a for loop instead of writing my original code and copying it 5x to complete the stat allocation process.
I have this right now.
if intro.lower().strip() == "yes":
print("The stats are: Intellect, Strength, Persuasion, Stamina")
stat_default = 5
stat_pool = 10
intellect = 0
strength = 0
persuasion = 0
stamina = 0
stats = [intellect, strength, persuasion, stamina]
print("Your stats cap at 10.")
while stat_pool >= 0:
for i in stats:
stats = int(input("Enter number. This is a test for stats."))
if stats > 5 or stats < 0:
stats = 0
stats = int(input("This is a test for invalid stat allocation"))
print(i)
stats = stats stat_pool
stat_pool = stat_pool - stats
print("Your current stats are {}".format(stats))
CodePudding user response:
To fix this you I added names to your stats array, in an array with the stat. I then iterate through the array and use the name to get input from the player. I add to the stat and take from the stat pool.
print("The stats are: Intellect, Strength, Persuasion, Stamina")
stat_default = 5
stat_pool = 10
intellect = 0
strength = 0
persuasion = 0
stamina = 0
stats = [["interllect", intellect], ["strength", strength], ["persuasian", persuasion], ["stamina", stamina]]
for elem in stats:
add = int(input(f"How much would you like to add to {elem[0]} out of a possible {stat_pool}: "))
elem[1] = add
stat_pool -= add
print(stats)
A quick note: I use f-strings in this example because it is the more up to date way to implement variables in strings. You might want to implement this in your full code, although I don't know if its totally worth it.