Python Compared with NQC

Program Blocks-- Python makes you an indention servant

A program block is a sequence of statements that belong together, e.g., the statements subordinate to an if or a while statement:

// NQC Code 1
int counter;
counter = 3;
while (counter>0)
{

Play_Sound(BEEP);
counter=counter-1;

}

As in the above code, the NQC language uses curly brackets {} to denote the begin-end of a program block.

If there are no curly brackets, the next statement after the condition is considered the subordinate program block.

As you may have learned, it is good to indent just for program readability, but tabs and other whitespace are ignored by NQC.

The Python language has no curly brackets. It uses indentation, generally tabs, to denote blocks. It also requires a colon after each condition:

# Python Code 1
counter = 3;
while (counter>0):

print counter
counter=counter-1;

Questions

1. How many beeps will play for the NQC code in the example 1 at top above?
2. How many beeps will play for the following NQC code:

// NQC Code
int counter;
counter = 3;
while (counter>0)

Play_Sound(BEEP);
counter=counter-1;

3. What will be printed for the Python code 1 above:

 

4. What will be printed for the following Python code:

# Python Code 1
counter = 3;
while (counter>0):

print counter

counter=counter-1;

NQC uses && and || for 'and' and 'or' in conditions.

while (x>0 && (y>0 || z>0))

Python, believe it or not, uses 'and' for 'and' and 'or' for 'or'. Hmmm...

while (x>0 and (y>0 or z>0))

Python has if, else, and elif.

Variables-- No Declaration (of Love) Necessary

In the NQC language, a programmer must declare the type of a variable before using it, e.g.,

int greencount; // this declares the variable greencount as an integer and sets up a memory box

greencount = 0; // this puts something in the memory box named greencount

In Python, you can just start using variables without declaring anything:

greencount=0 # this creates memory cell for greencount and puts 0 in it.

Comments and Semi-Colons

As the above code shows,

With NQC, anything after // is a comment.

With Python, anything after a # is a comment

With Python, no semi-colons are necessary after statements.

Teacher Demo: Computing GPA

In-Class Assignment

1. Copy your account balance program to another file (e.g., balance2.py). Change the program so that accounts with more than $10,000 pay a rich person fee of $100 yearly (the new balance computed after a year should show this).

2. Write a program factorial which takes in a number from the end-user (using the input() function), and prints out the factorial of that number. Hint: you'll need a while loop.