简体   繁体   中英

"Not enough Points" Python

ST = 5
statAdd = 5

There was more that I had typed, but none of it was relevant, so I just copied this.

while statAdd > 0:
    addStat = raw_input("""You may distribute %s points to your base stats. Which do you add to?
""" %(statAdd))
    if addStat == 'strength':
            pointDeduction = raw_input("How many points do you wish to add to Strength? (Up to %s points)" %(statAdd))
            if pointDeduction <= statAdd:
                    ST += pointDeduction
                    statAdd -= pointDeduction
            else:
                    print "You do not have that many points to distribute to Strength."

You would think that it should add the points, but I keep getting the same error "You do not have that many points to distribute to strength" when I obviously do. What am I doing wrong here?

Try to convert the inputs to to int ? Otherwise it's a string and doing arithmetic operations on it will lead to unexpected results.

     while statAdd > 0:
    addStat = int(raw_input("""You may distribute %s points to your base stats. Which do you add to?  """ %(statAdd)))
    if addStat == 'strength':
            pointDeduction = int(raw_input("How many points do you wish to add to Strength? (Up to %s points)" %(statAdd)))
            if pointDeduction <= statAdd:
                    ST += pointDeduction
                    statAdd -= pointDeduction
            else:
                    print "You do not have that many points to distribute to Strength."

raw_input returns a string.

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

Your if pointDeduction <= statAdd: statement is doing this comparison:

"5" <= 5

This will return False

To fix the problem, you need to wrap your input statement with int (if you are only allowing whole numbers).

addStat = int(raw_input("""You may distribute %s points to your base stats. Which do you add to?  """ %(statAdd)))

In python 2.x, raw_input() returns a string, thus performing things on it like that won't work. You should use plain input() instead (on the second one) which in Python 2.x automatically performs eval on input:

while statAdd > 0:
addStat = raw_input("""You may distribute %s points to your base stats. Which do you add to?""" %(statAdd))
if addStat == 'strength':
        pointDeduction = input("How many points do you wish to add to Strength? (Up to %s points)" %(statAdd))
        if pointDeduction <= statAdd:
                ST += pointDeduction
                statAdd -= pointDeduction
        else:
                print "You do not have that many points to distribute to Strength."

That should work out. Note, in Python 3.x, input() returns a string, while eval(input()) would return the int.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM