-1

I am creating a text based RPG where the player starts with 600 skill points to allocate between 6 skills.

So I start by assigning the value 600 to the skill_points variable.

skill_points = 600

then I assign a default value for each skills variable.

skill_1 = 0 skill_2 = 0 skill_3 = 0

Now I ask the player for input for the first skill.

skill1_input = int(input("Skill 1:"))

And I update the value of the variables.

skill_1 = skill_1 + skill1_input

skill_points = skill_points - skill1_input

Then I use a if statement to check if the number submitted is above the leftover skill points available, If not It prompts you to input the next skill.

if skill1_input > skill_points:

print("Not Valid")

else:

skill_2input = int(input("Skill 2:"))

Nested IF/ELSE statements repeat throughout all 6 skills until you allocate all your points. It is in a while loop so that if you don't use all of your skill points, it starts over at the first skill.

However, it is very finicky. At first I put 100 points into each skill and it worked fine. When I put 100 points into the first skill then 200 into the next skill, it prints not valid, even though there 500 points left and 200 is not is not more then 500.

There are multiple similar scenarios where the math should work properly yet the program still prints not valid

What is the current way to do this? Should I have not designed it using if statements?

  • 1
    There isn't anything wrong with using conditional statements, if they are being *finicky* then the logic must be wrong somewhere. Sometimes it helps to go through it line by line with pencil and paper - you being the interpreter and recording results. ... [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – wwii Feb 22 '20 at 14:54
  • 1
    There is probably a name error in one of your assignments. That sort of repetitive code is very easy to get wrong. Consider using a list of 6 skills: `skill_list = [0,0,0,0,0,0]`. Every time you get a new value, recompute the sum of the list: `if sum(skill_list) > 600:` instead of keeping the sum in a separate variable. That is a sure source of bugs. – BoarGules Feb 22 '20 at 14:57

2 Answers2

0

This can be better done with a for loop instead of nested if statements, and I would use an array for the skills.

skill_points = 600
skills = []
skill_inputs = 0
for i in range(6):
    skill_input = int(input("Skill %i: "%(i+1)))
    if skill_inputs + skill_input > skill_points:
        print("Not Valid")
        break
    else:
        skills.append(skill_input)
        skill_inputs += skill_input
ccl
  • 2,378
  • 2
  • 12
  • 26
-1

I rewrote your code and its functional to what you asked, but as an advice, before writing code, make sure your program makes fully sense to what you want to build(even if you don't know the tools to build it yet. That is what makes you learn more!)

If your code repeats itself, theres probably a better way to write it. As you can see, that code basically repeats itself 6 times.

You could replace it with:

skill_points = 600
skill_base = []
skill = 0
for i in range(6):
    if sum(skill_base) == skill_points:
        print("Values stored successfully!")
        break
    if sum(skill_base) > skill_points:
        print("Not valid")
        break
    skill = int(input(f'skill_ {str(i+1)}: '))
    skill_base.append(skill)
if sum(skill_base) == skill_points:
    print("Values stored successfully!")
savingDuck468
  • 347
  • 2
  • 6
  • The edited code is definitely a big improvement, I would suggest completely removing the verbose version especially when giving advice to beginners – mousetail Feb 22 '20 at 15:36
  • what do you mean by verbose? – savingDuck468 Feb 23 '20 at 14:50
  • Verbose: containing more words than necessary : wordy (Meriam Webster definition) Generally used to refer to code that is significantly longer than it could be. Note that in programming verbosity is not always bad, but in this case there is a lot of duplication which is error prone. – mousetail Feb 23 '20 at 15:12