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

From OpenHatch wiki
Content added Content deleted
imported>Jesstess
imported>Jesstess
No edit summary
Line 14: Line 14:


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:
<br />

[[File:Cbat_openingscreen.png|border|center]]
[[File:Cbat_openingscreen.png|border|center]]
<br />

This screen describes the problem (write a function to add any two numbers together) and gives you the inputs that CodingBat will give to the function you've written, and the outputs that CodingBat expects to see returned from your function for each input.
This screen describes the problem (write a function to add any two numbers together) and gives you the inputs that CodingBat will give to the function you've written, and the outputs that CodingBat expects to see returned from your function for each input.



Revision as of 22:51, 29 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 describes the problem (write a function to add any two numbers together) and gives you the inputs that CodingBat will give to 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:

For this example CodingBat is going to run your function three times (one for each set of inputs). Some of the CodingBat problems will run your function more than three times, some less.

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

Let's add some code. Remember that you just need to write the body of the function, but you still need to indent. Here's a first try at the problem:

Hmm. I've got one correct and two wrong. Maybe instead of return I should use print?

That's not it. CodingBat problems always want you to use return in your functions. Oh! I see - I typed in first twice, when instead I should have used second:

Great!

I can check this by typing the same function into the command prompt:


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

awf$ python
Python 2.6.1 (r261:67515, Aug  2 2010, 20:10:18) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def sumTwoNumbers(first, second):
...    return first + second
... 
>>> sumTwoNumbers(2,3)
5
>>> sumTwoNumbers(5,5)
10
>>> sumTwoNumbers(10,-10)
0

Perfect!