Boston Python Workshop 6/Friday/CodingBat Using Codingbat: Difference between revisions

From OpenHatch wiki
Content added Content deleted
imported>Jesstess
No edit summary
imported>Adamf
 
(5 intermediate revisions by one other user not shown)
Line 1: Line 1:
We are going to use a website called CodingBat.com to practice Python throughout this workshop.
We are going to use a website called [http://codingbat.com/home/bostonpythonworkshop@gmail.com/PythonIntro CodingBat.com] to practice Python throughout this workshop.


Before you do the CodingBat questions for the Friday tutorial, let's walk through how to use CodingBat.com with an example.
Before you do the CodingBat questions for the Friday tutorial, let's walk through how to use CodingBat.com with an example.
Line 11: Line 11:
We like CodingBat because it gives you immediate feedback on how close your function is to being correct.
We like CodingBat because it gives you immediate feedback on how close your function is to being correct.


Let's walk through an example: [http://codingbat.com/prob/p216579 the sumTwoNumbers] exercise.
Let's walk through an example, the [http://codingbat.com/prob/p216579 sumTwoNumbers] exercise.


If you visit that exercise web page, this is what you'll see:
If you visit that exercise web page, this is what you'll see:
Line 23: Line 23:
This screen:
This screen:
# describes the problem (write a function to add any two numbers together)
# describes the problem (write a function to add any two numbers together)
# shows you the inputs that CodingBat will use to test the function you've written, and the outputs that CodingBat expects to see returned from your function for each input.
# shows you some of the inputs that CodingBat will use to test the function you've written, and the outputs that CodingBat expects to see returned from your function for each input.


The inputs are the values in the parentheses, and the expected output is the value pointed to by the arrows:
The inputs are the values in the parentheses, and the expected output is the value pointed to by the arrows:
Line 31: Line 31:




If you simply click "Go" without typing anything in the box, CodingbBat gives you an error on the right hand side of the screen:
If you simply click "Go" without typing anything in the box, CodingBat gives you an error on the right hand side of the screen:




Line 49: Line 49:




Great! All the tests are green, so we know that we've done this problem correctly and can move on to the next one.
Great!



What CodingBat is doing is the same as when you write a function in your text editor or at the Python prompt and then run it a few times, like this:
What CodingBat is doing is the same as when you write a function in your text editor or at the Python prompt and then run it a few times, like this:
Line 68: Line 67:
Perfect!
Perfect!


Now that you are a CodingBat master:
<div style="font-size:125%">[http://codingbat.com/home/bostonpythonworkshop@gmail.com/PythonIntro Now we can move on to completing tonight's practice problems]</div>

<div style="font-size:125%">[http://codingbat.com/home/bostonpythonworkshop@gmail.com/Friday &raquo; Click here to completing tonight's practice problems &laquo;]</div>

Latest revision as of 02:43, 30 March 2012

We are going to use a website called CodingBat.com to practice Python throughout this workshop.

Before you do the CodingBat questions for the Friday tutorial, let's walk through how to use CodingBat.com with an example.

Using CodingBat

CodingBat is a little different from using Python at the command line or in a text editor like we've been doing. When you use CodingBat, you type your code into a web page and click 'Go' when you want that code to run. You'll still need to make sure you indent all your code to the same level.

For each CodingBat question, you will write a function. CodingBat will run your function with a few different inputs, and will compare the output of the function you wrote to what it knows is the correct answer. If all the outputs are correct for all the inputs, you've written the function correctly!

We like CodingBat because it gives you immediate feedback on how close your function is to being correct.

Let's walk through an example, the sumTwoNumbers exercise.

If you visit that exercise web page, this is what you'll see:



This screen:

  1. describes the problem (write a function to add any two numbers together)
  2. shows you some of the inputs that CodingBat will use to test the function you've written, and the outputs that CodingBat expects to see returned from your function for each input.

The inputs are the values in the parentheses, and the expected output is the value pointed to by the arrows:



If you simply click "Go" without typing anything in the box, CodingBat gives you an error on the right hand side of the screen:



Let's add some code. The function signature (the def sumTwoNumbers(first, second): part) has already been written for you. You'll write the rest of the function -- remember to indent everything inside the function, and remember to return your result instead of printing it!



Hmm. I've got one correct and two wrong. Oh! I see - I typed in first twice, when instead I should have used second:



Great! All the tests are green, so we know that we've done this problem correctly and can move on to the next one.

What CodingBat is doing is the same as when you write a function in your text editor or at the Python prompt and then run it a few times, like this:

>>> def sumTwoNumbers(first, second):
...    return first + second
... 
>>> sumTwoNumbers(2,3)
5
>>> sumTwoNumbers(5,5)
10
>>> sumTwoNumbers(10,-10)
0

Perfect!

Now that you are a CodingBat master: