Skip to content

Instantly share code, notes, and snippets.

@bgschiller
Last active September 15, 2017 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgschiller/4861d3a91e5770cad77afaea7531bab6 to your computer and use it in GitHub Desktop.
Save bgschiller/4861d3a91e5770cad77afaea7531bab6 to your computer and use it in GitHub Desktop.
def pos_or_neg(value):
if value > 0:
print("positive")
elif value < 0:
print("negative")
print("expecting positive... ", end='')
pos_or_neg(3)
print("expecting negative... ", end='')
pos_or_neg(-8)
print("expecting nothing... ")
pos_or_neg(0)
def print_positives(list_of_ints): # list_of_ints = [5, 0, 10, -2, -8, 4]
for value in list_of_ints: # 5, 0, 10, -2,...
if value > 0:
print(value)
def print_negatives(list_of_ints):
for value in list_of_ints:
if value < 0:
print(value)
print("I'm on line 19")
a_list = [5, 0, 10, -2, -8, 4]
print_positives(a_list)
print("I'm on line 21")
other_list = [-5, 18, 21, -12, 0, 0, 2, 13]
print_positives(other_list)
def print_certain_numbers(list_of_ints, which_nums):
if which_nums == 'positive':
# print the ones that are more than zero
print_positives(list_of_ints)
if which_nums == 'negative':
# print the ones that are less than zero
print_negatives(list_of_ints)
print("I'm on line 40")
print_certain_numbers(other_list, "positive") # should print 18, 21, 2, 13
print("I'm on line 42")
print_certain_numbers(other_list, "negative") # should print -5, -12
import turtle
def draw_bar(t, height):
""" Get turtle t to draw one bar, of height. """
if height < 0:
t.write(' ' + str(height))
t.begin_fill() # start filling this shape
t.left(90)
t.forward(height)
if height >= 0:
t.write(' ' + str(height))
t.right(90)
t.forward(40)
t.right(90)
t.forward(height)
t.left(90)
t.end_fill() # stop filling this shape
def main():
data = [48, 0, 117, -200, 240, -160, 260, 220]
max_height = max(data)
num_bars = len(data)
border = 10
wn = turtle.Screen() # Set up the window and its attributes
wn.setworldcoordinates(0-border, 0-max_height-border, 40 * num_bars + border, max_height + border)
wn.bgcolor("lightgreen")
tess = turtle.Turtle() # create tess and set some attributes
tess.color("blue")
tess.fillcolor("red")
tess.pensize(3)
for x in data:
draw_bar(tess, x)
wn.exitonclick()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment