-2
if int(gtin) == barcode[1]:
    print "You have ordered", item[1]
    **quantity1 = raw_input("Please select a quantity of 100mm bolts")
    quantitybolt = int(quantity1)
    quantity1 += quantitybolt**

TypeError:cannot concatenate 'str' and 'int' objects. Python

Please help :)

  • I think you want to add `int(quantity1)` to a different variable. The variable `quantity1` is the string returned by `raw_input`, it doesn't make sense to add its integer to itself. – Alasdair Apr 07 '16 at 12:46

3 Answers3

0

As you do not appear to use quantitybolt anywhere, you can convert the value returned from raw_input() directly to an integer using int() as shown below:

if int(gtin) == barcode[1]:
    print "You have ordered", item[1]
    quantity1 = int(raw_input("Please select a quantity of 100mm bolts"))

Now quantity1 will be set to the integer representing the user's input which is, I guess, what you want. You might like to wrap that in a try/except block to catch invalid input.

mhawke
  • 84,695
  • 9
  • 117
  • 138
0

The problem is obvious :

quantity1 = raw_input("Please select a quantity of 100mm bolts")
# here quantity1 is a string 
quantitybolt = int(quantity1)
# here quantitybolt is an integer - at least if no exception popped
# and now you try to add `quantity1` (which is a string) 
# to `quantitybolt` (which is an integer) - hence your error...
quantity1 += quantitybolt

Now since I don't have the slightest idea of what you're trying to achieve, I just can't tell how to solve your problem.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
-1

You have to convert you quantity1 (which a string) into an integer like they way you did for quantitybolt in the line above.

quantity1 = int(quantity1) + quantitybolt
Priyav Shah
  • 111
  • 1
  • 6
  • Which could be expressed without `quantitybolt`... – bruno desthuilliers Apr 07 '16 at 12:49
  • What do you mean? @brunodesthuilliers, Like the way you have done above? But that still results in a TypeError :P – Priyav Shah Apr 07 '16 at 13:13
  • There's no "way I've done above" - the code snippet in my answer is only the OP's code commented to explain why he gets this error. Since I don't have the slightest idea of what the OP _really_ tries to do there's no use in adding anything to the answer. – bruno desthuilliers Apr 07 '16 at 13:22
  • wrt/ the "what do you mean" : `quantity1 = sum(map(int, (quantity1, quantity1)))` could be an option, as well as just `quantity1 = int(quantity1) + int(quantity1)` – bruno desthuilliers Apr 07 '16 at 13:25
  • I hear you @brunodesthuilliers, I was just hurt that my code was being criticized but I have understood that its always useful to see criticism in a good light. Thank you for your input – Priyav Shah Apr 07 '16 at 14:37