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


#1

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!


#2

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"
True
>>> 
>>> x = "A"
>>> x in "aeiouAEIOU"
True
>>> 

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.


#3

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.


#4

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.


#5

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:

'p'
'pu'
'pur'
'purp'
'purpl'
'purple'

You should only be creating the last one


#6

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?


#7

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


#8

Google. Right. That.

Thank you for tolerating my foolish questions!


#9

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


#10

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

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


#11

#12

Sorry; not following.


#13

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


#14

OH! Following. I understand.


#15

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)


#16

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.

image

I may or may feel incredibly disappointed. Haha.


#17

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.


#18

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?


#19

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.


#20

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):
image