Saturday, November 1, 2014

Programming--No Syntax Errors!

Yesterday I tried to write my own program and it ran the first time I tried it with no syntax errors! This is a with any program I've tried, even when I'm following directions. Of course, the program still didn't do what I wanted...

This week's topic was functions and I don't understand them that well so on Saturday I tried making my own program that included a function. A very simple function. In fact, I just used one of the chapter examples. I wrote down some code in a notebook and then tried it on my computer, making sure all the colons were in the right places.

The program was just to take your birth month and birth day and add them together for a "secret number." The function was adding two numbers together.

Surprisingly, it started running no problem. The prompts came up. But when the answer showed up it was "716" which meant it was adding the numbers as strings instead of integers. So I went back and changed the line int(month) to month = (int)month, ran the program again, and got the answer I wanted, which was 23.

However, the program still didn't work the way I wanted it to because of the error messages. I had put in a try/except clause, but the program just kept going when I entered weird information, so it gave something like "Julyquit" as the final result. So I added quit() to the except clause and that stopped the program every time I entered something that couldn't be converted into an integer.

Overall, it was a good feeling to be able to use a function correctly. And even though I still had to do some debugging, I felt very knowledgeable to be able to think, "Ohhhh it didn't actually convert to an integer," or "I need an extra line of code here," instead of "OH NO HOW DID I SCREW IT UP?????"

Next week is loops, which I also know nothing about but hopefully I'll get the hang of it eventually.

No comments:

Post a Comment