2017-02-03 HW

posted Feb 3, 2017, 7:11 AM by Samuel Konstantinovich
Goal: Function in python

If you have not shared your Codingbat with me, please do so in your preferences: konstans@stuy.edu

Variable and function names in python:
    1. Start with a letter or underscore
    2. The rest of the characters can be letters, numbers, and underscores.
      OneFish
      blueKitten
      _what_
      x2
      c3p0
      y_u_so_n00b


Here you can find a short warmup problem set:

-The link is here, but you should read the rest of the warmup notes below it.

-Testing code is important, codingbat does it for you. Try testing your code in IDLE, by using the test cases that codingbat provides. Keep your IDLE window open, and your test cases displayed. You can always test more problems later! (You will be more likely to do this if you have IDLE running and ready)

-These codingbat problems are easy enough to write in the codingbat window. This does not give you all the features of IDLE, so if you don't get the right answer, you can get better error messages and parenthesis matching in IDLE.




NEW STUFF

If statements in python work the same way they did in scheme and netlogo:

if a:
   statement1 #only executes when a is true

statement2 #executes all the time because it is not inside of the if




if a:
   statement1 #execute when a is true
else:
   statement2 #execute when a is false

statement3 #executes all the time because it is not inside of the if


Since an if statement can be put inside another if statement we have to be careful. Different ways of connecting them can produce different effects. (discussed in class)

if a:
   statement1
else:
   if b: 
      statement2

OR...


if a:
   if b: 
      statement1
else:
   if c: 
      statement2

OR...

if a:
   if b: 
      statement1
   else:
      statement2
else:
   if c: 
      statement3

OR...

if a:
   if b: 
      statement1
   else:
      statement2
else:
   if c: 
      statement3
   else:
      statement4
 



You may have noticed that if/else statements that are connected start to indent too much.
if a:
   statement1
else:
   if b: 
      statement2
   else:
      if c:
          statement3
      else:
          statement4

These statements can be rewriten using elif (else+if) as follows.

if a:
   statement1
elif b: 
   statement2
elif c:
   statement3
else:
   statement4


HOMEWORK 

Complete: Coding bat problems 
Warmup-1
  pos_neg
Logic-1
  sorta_sum
  near_ten
  love6
Logic-2
  lone_sum
  lucky_sum
  no_teen_sum





Comments