
|
Lesson 13 Notes: Gettin' the Gotchas (A Review)
- Plan Ahead
- Plan before you code.
- Create and write out "pseudocode"
- Get structure planned before programming
- Choose the best structure:
- list or string?
- Function or while loop?
- Avoid obfuscational complexity
Why make it messy when you can make it simple?
- Mutability
- Lists can be changed without reassigning them.
list = ["a", "b", "c"]
list.append("d")
- Strings cannot be changed without reassigning
text = "I Like Mommy"
text = string.upper(text)
- Values in Functions
- If a function needs a variable not created inside the function, pass it in:
word = raw_input("Enter a word: ")
def word_cap(word):
print string.capitalize(word)
word_cap(word)
- Fruitful Functions
- If your function needs to give a value back to the main program, give it a handle
def find_lcm(num1,num2):
index = 1
while 1:
mult1 = num1* index
mult2 = num2*index
if mult1 == mult2:
return mult1
lcm = find_lcm(num1,num2)
- Recursive Alternative
- Instead of recursing a function, use a while loop and "break" out of it:
while 1:
pwd = raw_input("Password: ")
if pwd != "it's-a-secret":
print "Incorrect. Entry denied."
else:
break
- Etcetera, Etcetera, Etcetera
- Before submitting a program, make sure it runs without any traceback errors.
- IDLE Tricks:
- Mass Indent / De-dent
- Mass Comment / Uncomment
- Tutor List
Restricted access |