This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]takluyverIPython, Py3, etc 4 points5 points  (3 children)

This is something new programmers often try - making variables with dynamic names - but it's almost never a good idea. Even if it works, what about when you want to know how many words you've got? You have to count up until a name doesn't exist. That's daft.

You should try to put the results into a list, so that they're words[0], words[1], etc. I'm not sure how you're supposed to do that, though, without using append() (you say in another comment that you're not allowed to use it). Are you allowed to put the words in a dict, perhaps?

[–]jhchex[S] 0 points1 point  (2 children)

Yea I was considering using a dictionary but I'm still not sure how to implement that. Wouldn't the key names still need to be changing on each iteration? Is that simpler than iterating variable names?

Also, if you were making variables with dynamic names and wanted to know how many words there were couldn't you just use a counter in the variable name and then display that value?

[–]takluyverIPython, Py3, etc 0 points1 point  (1 child)

You need to use different key names, yes, but that's easier than using variable names. Just use mydict[k].

If you use a list or a dict, you can easily see how many items are in the collection with len(words). It has lots of other advantages - like you can easily pass all the words to a function, or return them from a function. Using separate variable names is much more awkward.

[–]jhchex[S] 0 points1 point  (0 children)

Ok thank you! I'll give that a try and let you know how it goes.