I recently found myself with some unanticipated extra time on my hands, so I decided to put it to good use and get through another chapter of Learn to Program by Chris Pine.
If you're a beginner, I highly recommend going through his *free* e-book (also offered as an actual paper book). It.is.wonderful. An actual tutorial that helps you to do more complex problems as opposed to the ol' "Hello, world" and "2 + 2" examples--who knew you could do so much!! (Kidding.) Really though, his writing and teaching method is so clear and succinct that it was able to help a noob (me) understand the basics of Ruby AND put it to use.
As promised, I'm going to show you the problem and walk you through my answer. Don't look if you haven't tried this yet yourself!! Apparently that's half the fun...
Here's the prompt:
Here's my program:
I set bottles to 99 because that's the starting point. We start at 99 and count down from there.
Because of the song's repetitive nature, I knew I would want a loop so I didn't have to manually type out every line of the song. I tried while bottles > 0 and while bottles != 0, and both gave me the answer I wanted.
I put the first two lines of the song first, because we have to start at 99.
If you use double quotations you can use the #{ } inside the quotations to call on the variable bottles, but this will not work with single quotations.
* Alternatively you could use string concatenation and write:
puts bottles.to_s + 'bottles of beer on the wall..'
Then I put bottles = bottles - 1 because the line after the first to has to include one less bottle than what the previous bottles variable stored (99).
The next part is an if/else statement we need to embed in our while loop because one bottle, not one bottles.
I proceeded by saying if bottles == 1 then puts "#{ } bottle of beer..."
else (if bottles == anything other than 1(don't need to write)) puts "#{ } bottles of beer..."
Don't forget to put end.
That next little line puts ' ' just makes sure that we have a line break after each iteration of the song so that it's a bit easier to read. It obviously is not necessary.
So now if you try to run this program it will almost work. We took care of everything except the very last iteration of the song. It's should still say "1 bottles of beer on the wall..."
We need one more if statement that will modify this last part of the song, which is what you see above. No else needed.
Whew. That was a lot. I know this may seem pretty easy, and once you get a handle on things it really is... but it was also really easy to get tripped up on some little things. If you have any questions or maybe a better way to do this same thing, send'em my way!
Until next time, friends.
Thursday, February 25, 2016
Learn to Program: 99 Bottles of Beer
Labels:
99 bottles
,
chris
,
chris pine
,
clever
,
cleverett
,
coder
,
coder girl
,
girl
,
learn to program
,
pine
,
program
,
ruby
Tuesday, February 23, 2016
Hello, World!
Could there be a more fitting title for the *first* post of your *first* blog to document your coding journey? I didn't think so. But anyways, welcome!
My name is Christine and I am an aspiring girl coder. My hope is that all of my hard work for the next couple of months will wield me the knowledge and awesomeness to enter a bootcamp *cough cough Grace Hopper or app Academy cough cough** where I'll really be able to start learning from professionals and other people my age.
I'll be linking to all my social media sites soon and adding my portfolio as well. In the mean time, feel free to contact me at cleverett1 at gmail dot com.
Until next time, invisible internet friends!
My name is Christine and I am an aspiring girl coder. My hope is that all of my hard work for the next couple of months will wield me the knowledge and awesomeness to enter a bootcamp *cough cough Grace Hopper or app Academy cough cough** where I'll really be able to start learning from professionals and other people my age.
I'll be linking to all my social media sites soon and adding my portfolio as well. In the mean time, feel free to contact me at cleverett1 at gmail dot com.
Until next time, invisible internet friends!
Subscribe to:
Posts
(
Atom
)