WEEK03: booleans, conditionals (if/else)
----------------------------------------
F: review expressions, if/elif/else, comparison operators
FUN:
http://en.wikipedia.org/wiki/George_Boole
POWER vim USERS:
all of these should be run from command mode (hit Esc to switch to command mode)
F9 runs your python program (without leaving the editor)
dd delete a line
5dd delete 5 lines
p paste (below where cursor is)
yy copy a line
u undo (multiple levels, if needed)
Ctrl-r redo
shift-v select text using arrow keys
> or < to shift selected text left or right
:colorscheme blue switch to blue colorscheme (add to .vimrc file to make permanent)
CONDITIONAL STATEMENTS:
if < condition > :
do this
and this
and anything indented to this level
else:
do that
and that
EXAMPLE:
speed = input("\nHow fast were you going? ")
if speed > 55:
print "that's too fast....SLOW DOWN!!!!!!!!!!!!!"
print "here's your ticket\n"
else:
print "OK...off you go.\n"
WRITE YOUR PROGRAMS IN STAGES!!
- write a small part of your program, then test it
- once it works, add another small part, then test it again
- and so on...
Here's an example:
$ python multiply1.py
Let's test your multiplication skills...
What is 3 x 7 ? 20
Incorrect....the answer was 21
$ python multiply1.py
Let's test your multiplication skills...
What is 3 x 7 ? 21
CORRECT!!
- this program always asks 3 x 7
- not much fun yet, but let's get that to work, then add to it!
"""
program to test user multiplication skills
J. Knerr -- Fall 2012
"""
# ------------------------------------------------------ #
def main():
"""ask user question, give feedback on user answer"""
print "\nLet's test your multiplication skills...\n "
a = 3
b = 7
answer = a * b
question = "What is %d x %d ? " % (a,b)
useranswer = input(question)
if useranswer == answer:
print "CORRECT!!\n"
else:
print "Incorrect....the answer was %d\n" % (answer)
# ------------------------------------------------------ #
main()
- note use of variables a, b, and answer
- use variables!! makes code easy to read and more efficient
- also can make code easy to update, like this:
$ python multiply2.py
Let's test your multiplication skills...
Please enter a factor: 5
What is 5 x 7 ? 35
CORRECT!!
$ python multiply2.py
Let's test your multiplication skills...
Please enter a factor: 6
What is 6 x 7 ? 42
CORRECT!!
- now we are asking the user for a factor, then testing them
on factor x 7
- the only difference between this new code and the one above?
change a = 3
to a = input("Please enter a factor: ")
# --------------------------------------------------- #
ADD RANDOM NUMBERS TO YOUR PROGRAMS:
>>> from random import *
>>> x = randrange(1,11)
>>> print x
2
>>> x = randrange(1,11)
>>> print x
9
>>> x = randrange(1,11)
>>> print x
3
>>>
>>> item = choice(["apple", "orange", "lemon"])
>>> print item
apple
>>> item = choice(["apple", "orange", "lemon"])
>>> print item
apple
>>> item = choice(["apple", "orange", "lemon"])
>>> print item
lemon
>>> item = choice(["apple", "orange", "lemon"])
>>> print item
orange
- change the above multiply code to test the user on factor x random_number:
$ python multiply3.py
Let's test your multiplication skills...
Please enter a factor: 5
What is 5 x 9 ? 45
CORRECT!!
$ python multiply3.py
Let's test your multiplication skills...
Please enter a factor: 5
What is 5 x 5 ? 25
CORRECT!!
# --------------------------------------------------- #
- now let's put this in a LOOP and ask the user 3 multiplication questions:
$ python multiply4.py
Let's test your multiplication skills...
Please enter a factor: 5
What is 5 x 5 ? 25
CORRECT!!
What is 5 x 2 ? 10
CORRECT!!
What is 5 x 4 ? 20
CORRECT!!
# --------------------------------------------------- #
- if you have time: keep track of how many they get correct and
include a message at the end, based on how well they did:
$ python multiply5.py
Let's test your multiplication skills...
Please enter a factor: 5
What is 5 x 10 ? 50
CORRECT!!
What is 5 x 2 ? 10
CORRECT!!
What is 5 x 5 ? 25
CORRECT!!
Excellent work!!
$ python multiply5.py
Let's test your multiplication skills...
Please enter a factor: 5
What is 5 x 10 ? 1
Incorrect....the answer was 50
What is 5 x 4 ? 1
Incorrect....the answer was 20
What is 5 x 7 ? 1
Incorrect....the answer was 35
Not so good...you need to run the program again! :(
# --------------------------------------------------- #
NOTE: for this last part you need if/elif/else branching:
if nright == nprobs:
print "Excellent work!!"
elif nright > nprobs/2:
print "Good job. Keep practicing!"
else:
print "Not so good...you need to run the program again! :( "