anonymous
  • anonymous
I'm new to Python. How how can I append a counter to a variable? ctr = 3 variable = variable3
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
What are you trying to do? I don't quite understand the question. If you are trying to add 3 to any variable you can just use variable +=3 So, >>> ctr = 0 >>> ctr += 3 >>> ctr += 3 >>> print ctr 6
anonymous
  • anonymous
variable = [] variable.append(ctr) ??????
anonymous
  • anonymous
when you tried it did you get something like this in the traceback?: TypeError: cannot concatenate 'str' and 'int' objects maybe this: v = "variable" + str(ctr)

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
If I'm understanding the question correctly, you are trying to programmatically create a variable name using information stored in another variable. While it is possible (with some magic) it's not recommended. Instead I would suggest storing the information into a dictionary or, if you are accessing sequential data, a list. e.g. variable = [] variable.append('ham') variable.append('eggs') variable.append('toast') print "For breakfast I'd like %s, %s and no %s" % (variable[1], variable[2], variable[0])
carlsmith
  • carlsmith
You can't. You just increment the value assigned to a variable ctr = ctr + 1 or ctr += 1 for short. You can assign the value of a variable to another. a = 5 ctr += a That's about it. The append method is for lists, for adding an object on to the end of an existing sequence of objects.

Looking for something else?

Not the answer you are looking for? Search for more explanations.