... and it includes a set of knives that can cut through a nail...
Computers are not the brightest beings on the planet. They are really good at handling instructions. This is important.
As a programmer, every single error a user encounters is your fault.
I'm not saying this to be funny or ironic. It really is, and if you can't assimilate that little fact then you're not going to make it. So get used to it now.
Write 'please' and 'thank you' into your outputs.
Sit down and test, test, test. Try to think of every way someone could confuse, baffle or mis-treat your code.
It will pay amazing dividends if you learn to be good at this.
So since computers basically just handle instructions, they REALLY depend on us to get the instructions right. And since they are not creative about it, they will always take those instructions one line at a time.
So we programmers talk about 'flow'. There are basically three kinds of flow you need to think about:
-sequential (one right after the other)
-selection (do something based on a condition, conditional)
-repetition (the computer's specialty)
So let's look at the first one: sequential. You have to keep in mind that the computer is going to read your instructions in sequence:
price = 2.50
tax = .08 (this is 8%)
cost = price * tax
print cost
First notice that when assigning a value to a variable, the variable always goes on the left of the assignment operator ( = ).
Second notice that the values for price and tax are assigned, BEFORE you multiply them together and assign the result to cost. If you wrote:
price = 2.50
cost = price * tax
tax = .08
print cost
you would get an error. Why?
Now if you are trying this out, I've left a couple errors in there.
The first error is that line three is missing something.
The second error is that python has no way to format the output for currency built into the main library. So instead of $2.70, we get 2.7
It's not a hard thing to fix, but a little more advanced. I'll get there.
If you're going to do anything in programming you're gonna need how to get input from the user also. Here's how we do it in python (from a console):
someText = raw_input("Please enter some text:")
Try it.
The point of this course is to make you a linguist, not teach you a language. I would like you to be able to understand what's going on behind all computer languages so that you can pick one up easily when you need it. To see the similarities in them all as an aid to debugging or trouble-shooting.
So the last problem was solved with python in a manner similar to what you see on the right:
But in an earlier lesson I asked that you pick a language and start acquainting yourself with it. Here at codecademy the online IDE only works with Ruby, Python or JS. There are many fine ones that work with other languages out there, including some that are not competitors of the nice people that are letting me use their service here.
Take a look at: ideone.com or compilr.com
If you are looking at C++ the solution to the last exercise is:
-------------------
#include <iostream>
using namespace std;
int main ()
{
float itemPrice, salesTax(.08), tip, finalCost;
cout << "Please input the price of the item: ";
cin >> itemPrice;
cout << "Please input the amount of the tip: ";
cin >> tip;
finalCost = (itemPrice * salesTax) + (itemPrice + tip);
cout << "You're total, including tax and tip, is " << finalCost;
return 0;
}
--------------
See it run here: http://ideone.com/VkKto
If you're interested in Java, it would be like this:
--------------
import java.util.Scanner;
class RestaurantBill
{
public static void main (String[] args)
{
Scanner scan = new Scanner( System.in );
double itemPrice, salesTax(.08), tip, totalCost;
System.out.print("Enter the item price: ");
itemPrice = scan.nextDouble();
System.out.print("Enter the tip amount: ");
tip = scan.nextDouble();
System.out.printIn("Your total purchase today, including tip and tax, is: " + ((itemPrice*tax) + (tip+itemPrice));
}
}
Mainly, it's important to realize at this point how the code is carried out sequentially, how variables and their types are declared and assigned, and how you can input and output information.
If you're working on something in another language, please share it with us.