Alternative Solution To My Lengthy Code? (8/15)


At the least, I was able to further ingrain Python’s colon-indentation syntax into my cranium. However, this was not my aim. Is there an alternative that is not as laborious? (…I am sure there is, but I ask for guidance on how to find this alternative.)

Thank you in advance!


Have you considered the in operator we use in loops? It can do some pretty neat stuff. Instead of for, try if, or while.

>>> "A" in "All the Diamonds"
>>> x = "A"
>>> x in "aeiouAEIOU"

All the Diamonds in the World
Bruce Cockburn

All the diamonds in this world
That mean anything to me
Are conjured up by wind and sunlight
Sparkling on the sea

I ran aground in a harbor town
Lost the taste for being free
Thank God He sent some gull-chased ship
To carry me to sea

Two thousand years and half a world away
Dying trees still grow greener when you pray

Silver scales flash bright and fade
In reeds along the shore
Like a pearl in sea of liquid jade
His ship comes shining
Like a crystal swan in a sky of suns
His ship comes shining

Not a statement, just where I got the line. Great song, though, as is the entire album it’s from. Look it up…

Salt, Sun and Time.


If you’re writing the same code over and over with different data, you can take that as a sure sign that a loop would do the job for you.

Some other things:

You have no need for an index, so rather than creating a list of numbers 0…len(text) and iterating through that, iterate through the text directly instead. (Otherwise you may as well create another list of numbers so that you can iterate through your list of numbers so that you can iterate through your string, and so on)

You’re using anywhere between 2 and 6 spaces for your indentation, the preferred amount for python is 4, but codecademy’s editor probably gives you 2 when pressing tab which you can’t change so you might not want to bother making it 4. Use the same amount everywhere though.

Strings don’t support modification (a new string is created), so while building your data you should be using a list (which does support adding values quickly at the end), and then when you’ve collected everything use str.join to create a string from that.


Ah! I am such a novice at Python - such a novice that, despite loops only being one lesson ago, they did not occur to me as an option. I appreciate the much-needed reminder.

I went ahead and did so. The code is MUCH more orderly. I am receiving a curious error message, however:

I am assuming that the “dcb” is a check for whether I am properly considering the length of text, but I will ask again for a bit of input (pun intended), if you could.


Your function will get used multiple times. That’s kind of the point of functions.

print anti_vowel('hello')
print anti_vowel('hello')
print anti_vowel('hello')
print anti_vowel('hello')
print anti_vowel('hello')

And the result should be the same each time (if the input is the same)

You’re still creating strings with +

To join together ‘purple’, you create all of these:


You should only be creating the last one


I appreciate your thorough response, @ionatan. I think my only question pertains to str.join - what is the proper syntax for this operation? I initially tried str.join(list_name), but evidently I was not successful (hence why I am writing here).

Incidentally, the indentations I added to the code are not my doing, really. I did add them, but if I opted to not include more indentations than necessary, the output would be an error message indicating that there should have been a block where there was none. I am assuming there is a glitch of some sort?


You may have been mixing tab characters and spaces. Use only spaces, and use the same amount everywhere.

str.join is a string method, you should be calling it on a string, in this case the string to put between each element. What you generally do is google for examples and/or documentation


Google. Right. That.

Thank you for tolerating my foolish questions!


Also, getting a reversed result doesn’t necessarily mean you should be reversing again.

Unless you meant to reverse as part of what you were doing, it probably shouldn’t have been reversed in the first place

Imaginary internet points for seeing the problem and coming up with a solution, but a deduction for bolting on more code instead of fixing the offending code


Shame. You’re keeping tally on me. :frowning:

I tried the str.join operation, edited a few doohickeys, but the error persists…



Sorry; not following.


Test your code similar to that (paste it in) and watch how the result changes with each call despite the input being the same each time


OH! Following. I understand.


I should also mention that:

  1. For small amounts of text, the time spent creating redundant strings is insignificant
  2. CPython (the reference implementation of python) cheats a bit and actually does change the string anyway, because it’s able to tell (in this particular case) that nobody will notice that it changed, that it won’t have any undesired effects. It’s a speed hack. However, if you use jython (runs on java’s virtual machine) or pypy (python written in python, it’s faster if you can believe it), they might not do that. And, aside from which implementations might or might not do it, I’m a big believer in writing what I mean, which is not to create all those strings.

Regarding indentation, it may be easier to use an external editor with sane behaviour if codecademy’s editor isn’t cooperating. If you’re already using a text editor that is meant for code, go with that, I just feel like I should name one while making the suggestion, so a simple well-behaved editor is gedit. Python also comes with its own toy editor IDLE, and windows users may prefer notepad++ (only runs on windows, mainly it’s just better than notepad, that’s all I have to say for it), no clue what mac users use, but I’m sure they have to pay for it, then there’s stuff like VSCode and Atom which are basically browsers that are only editors. Can’t suggest what I use because vim is completely different and new vim users can’t even figure out how to save and exit (forget your mouse, forget any editor keybindings you’re used to)


I have used Jupyterhub in classes last semester, so it’s a platform I am relatively comfortable using. I may transition to it if I find the repeated fixing of indentations to be too cumbersome.

Since my most recent reply, I have been trying various methods to repair the function. Following a modest HOUR, I found the trivial fix…behold.


I may or may feel incredibly disappointed. Haha.


Still some things to take care of though.
That’s certainly not 4 spaces per step, and how many times should you be doing join? Do you really need your string variable or could that value be used directly instead, avoiding having to look for its definition to figure out what it means (string doesn’t say much, empty_string would be better, but '' is as clear as it’ll ever get.

Yes, the function certainly shouldn’t have a memory that persists between calls and it shouldn’t really be changing anything outside itself.

Jupyter is pretty much powerpoint, isn’t it? It’s there to show things, not to build or run things. But if it does what you want I probably shouldn’t argue against it… Yet.


The spaces shown are the spaces allowed, unfortunately. Regarding the remaining corrections, which are more so for aesthetics than functionality (I think), I would definitely prefer two apostrophes to a definition. Noted!

I should have clarified that I use Jupyter Notebook (“Jupyterhub” was how my professor referred to it), and its use is effectively what Codecademy offers. Perhaps you are considering Github?


That’s not how it goes. Also, those are not just spaces (if you look at your screenshot, line 8 doesn’t line up with anything whatsoever. This isn’t something you shuffle around until it “works”. Indentation has meaning, and you put it exactly where you want it, and you use 4 spaces per indentation level.

Github is something else entirely, that’s for storing git repositories and facilitating communication around those repositories.

What I’m advocating is having your trusty text editor, and then running python on the file.


I assure you, the spacing is not a preference, nor is it syntactically correct. It is simply what the program “instructed.” In this instance, I did shuffle the indents around, because the proper indentation was ineffective.

Here is the code using Jupyter Notebook (along with your definition suggestion):