-1

I have this code already put into a larger chunk of code, but I've narrowed it down to this as the error. I know that the error is that I am attempting to add a variable to something in a dictionary. Is there any way that I could add it to the actual stat itself?

smallGuns = 5
bigGuns = 2
unarmed = 3
meleeWeapons = 20
throwing = 4
firstAid = 2
sneak = 5
lockpick = 10
steal = 3
science = 4
repair = 3
speech = 5

choice = raw_input("Which stat do you want to add points to?")
skillPoints = 5

statlist = ['small guns', 'big guns', 'unarmed', 'melee weapons', 'throwing', 'first aid' 'sneak', 'lockpick', 'steal', 'science', 'repair', 'speech']

if choice in statlist:
pointDeduction = input("How many points do you wish to add to %s? (Up to %s points)" %(choice, skillPoints))
if pointDeduction <= choice:
        choice += pointDeduction
        skillPoints -= pointDeduction
else:
        print "You do not have that many points to distribute to %s." %(choice)

print steal

My error message is

Traceback (most recent call last): File "F:/TARG/temp.py", line 22, in <module> choice += pointDeduction TypeError: cannot concatenate 'str' and 'int' objects
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
  • Can you post the actual error message? – michaelpri Jun 26 '15 at 00:37
  • Error message: Traceback (most recent call last): File "F:/TARG/temp.py", line 22, in choice += pointDeduction TypeError: cannot concatenate 'str' and 'int' objects – TyTheChosenOne Jun 26 '15 at 00:39
  • Searching for that error message, "TypeError: cannot concatenate 'str' and 'int' objects", would lead you straight to a variety of (StackOverflow) related answers and solutions. –  Jun 26 '15 at 01:14
  • Do [not use](http://stackoverflow.com/a/7710959) `input()` when using Python 2. –  Jun 26 '15 at 01:16

3 Answers3

0

Your example currently has no dictionaries. You have initizlied it wrong. It should be as follows:

statlist = {"attribute_name" : attribute_value, REPEAT}

Once you have the correct dictionary initalization

statlist = {'small guns' : 5, 'big guns' : 2, 'unarmed' : 3} # you do the rest
choice = raw_input("Which stat do you want to add points to?")
if choice in statlist:
    pointDeduction = input("How many points do you wish to add to %s? (Up to %s points)" %(choice, skillPoints))
    if pointDeduction <= statlist[choice]:
        statlist[choice] -= pointDeduction
        skillPoints -= pointDeduction
else:
    print "You do not have that many points to distribute to %s." %(choice)

You also have some logic issues when distributing points, but you can figure that out on your own.

AndrewGrant
  • 786
  • 7
  • 17
0

I'm guessing from your code that statlist is intended to be a dictionary of stat keys with stat value values. Right now you have a list, so essentially you're saying "if the item is in the list, concatenate a number to the end of it" (albeit incorrectly).

What you want to do is add dictionaries to the question. The first part, where you declare variables, is not exactly necessary, you could accomplish it like so:

statlist = {'small guns' : 5, 'big guns' : 2, ...}

For each value. And then, to change stats:

if choice in statlist:
    pointDeduction = input("How many points do you wish to add to %s? (Up to %s points)" %(choice, skillPoints))
    if pointDeduction <= statlist[choice]:
        statlist[choice] += pointDeduction
        skillPoints -= pointDeduction
else:
    print "You do not have that many points to distribute to %s." %(choice)
maccartm
  • 2,035
  • 14
  • 23
0

Collect your stats in a dict and use like so.

choice = raw_input("Which stat do you want to add points to?")
skillPoints = 5

statlist = {'small guns': 5, 'big guns': 2, 'unarmed': 3, 'melee weapons': 20, 'throwing':4, 'first aid':2, 'sneak': 5, 'lockpick': 10, 'steal': 3, 'science': 4, 'repair': 3, 'speech': 5}

if choice in statlist:
    pointDeduction = int(raw_input("How many points do you wish to add to %s? (Up to %d points)" %(statlist[choice], skillPoints)))

    if pointDeduction <= skillPoints:
        statlist[choice] += pointDeduction
        skillPoints -= pointDeduction
    else:
        print "You do not have that many points to distribute to %s." %(choice)

    print statlist[choice]
else:
    print 'you entered an invalid choice'

To print values you can do the following

# print an individual entry
print 'My small guns points are %d' % statlist['small guns']

# print all entries in a loop
print 'My points inventory is'
for key, value in statlist.iteritems():
    print '%s = %d' % (key, value)
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61