How to Program
Raw English Algorithm
Convert to Comments
Flesh out comments
Debug. Did you tell it what to do incorrectly or did you tell it to do the wrong thing? Bad
implementation can be googled, bag algorithm cant. Don’t solve problems you don’t understand. Don’t
start with the solution.
input() always returns a string, even if what you entered was a number
str() can be used to concatenate integer/float to a string
int() can be used to convert a string value to an int for use in mathematics, or can round a floating-point
number down
float() can get floating point version of a value
input() evaluates to an expression of whatever string the user typed in
len() evaluates to the integer value of the number of characters in a string
print() expressions can always evaluate to a single value; see input()
hence why inserting variable
+ can only add two data types of the same types (can’t add integer to string)
expressions evaluate to values
assignment statements store a value in a variable
Boolean values are used in expressions and can be stored in variables
Integer/Floating point values will always be unequal to a string
<, > , <=, >= work only with Integer/Floating
!= and == can work with string
not Operator can be like double negatives being nested
Comparison operators <, > , <=, >=, !=, == evaluate to Boolean values and can be used in expressions
with Boolean operators AND/OR/NOT (OoO: AFTER Math & Comparison Operators evaluate -> Not ->
And -> Or)
Math Operators OoO: **, %, //, /, *, -, +
Expressions are evaluated from left to right
Boolean expressions can be conditions, expressions in flow control statements that evaluate to a
Boolean value. The flow control statement decides what to do based on a True or False condition
Flow control Statements
if statement block executes if the statement’s condition is true, the block is skipped if false, “If
this condition is true, execute the clause”. Precede with else statement if necessary, which executes
when if statement is false- it doesn’t have a condition
if name == ‘Alice’:
print(‘Hi, Alice.’)
else:
print('Hello, stranger.')
elif statement is an ‘else if’ statement that follows another if or elif statement that provides another
condition to be checked if previous conditions were false; with a chain of elif statements, one or none of
the clauses executes, and for the former, the rest are automatically skipped once true condition has
been found, know that order matters. An else statement after the last elif statement guarentees one of
the clauses will be executed (there must be only one if statement)
while statement code block is executed as long as the statement condition is true; execution jumps back
to start of while statement
spam = 0
while spam < 5:
print('Hello, world.')
spam = spam + 1
break statement are used inside loops; upon reaching break statement, immediately exits while loop
continue statement are used inside loops; upon reaching continue statement AND end of the loop,
immediately jumps back to start of loop & revaluates the loop’s condition
o, o.o, ‘ ‘ are considered false when used in conditions, all other values are considered true
for statement and range() function can execute blocks of code only a certain amount of time; i goes up
to, but doesn’t include the integer passed to range (i is initially set to 0). Can change the integer passed
to range() to follow any sequence of integers by passing multiple arguments, even those that start at a
non-zero number (12, 16 -> 12, 13, 14, 15) and add a third argument as a step argument (0,10,2 -> 0 2 5
6 8) and EVEN use a negative number as a step argument to count down instead of up!
print('My name is')
for i in range(5):
print('Jimmy Five Times (' + str(i) + ')')
a while or for loop implicitly ends with a continue statement, much like how return None is added to the
end of any function definition with no return statement (or just the return keyword itself). None if often
used in return value of print() since its useful to store something that won’t be confused for a real value
in a variable- All functions must evaluate to a return value, but print() doesn’t need to return anything
the same way len() or input() does
import [module] can be preceded with from so calls to a function will not need the module’s prefix
def function() {body} defines a function (only calls to the function actually execute it); optional to use
parameter (variable an argument is stored in when a function is called) inside parenthesis to create a
function that accepts arguments. The value stored in a parameter is forgotten when the function returns
(calls are destroyed after logic returns- that’s the function’s local scope). Return value is the value a
function call evaluates to- specify what return value should be used with a return statement. Return
values can be passed as an arguement to another function call
while loop
sentry: variable that will control loop
initialization code: code that initializes sentry
condition: loop repeats if condition is true
change code: code to change sentry so condition can be triggered
while algorithm:
initialize sentry with initialization code
then continue loop as long as condition is true
inside loop, change sentry with change code
syntax only requires a condition and that condition must be satisfiable
for loop
sentry: integer vartiable that will control loop
start: integer value of sentry at beginning
finish: integer value of sentry at end
change: integer to add to sentry at each pass
for algorithm:
begin with sentry at start and add
change to sentry on each pass until sentry is larger than or equal to finish
Multiple Exits
Password loop that 1. exits with positive result if user chooses right password 2. exits with negative
result if user is wrong 3 times