Unit 5 Python: FUNCTIONS AND PARAMETERS (code)
5.1.5 Triple
# Enter your code here num=int(input("What's your number? ")) def triple(x): triple_x = x*3 print(triple_x) triple(num) #other calls to test function triple(8) triple(5) triple(100)
5.3.5 Graphics Stop Light
# This program should draw a stop light LIGHT_RADIUS = 25 STOPLIGHT_WIDTH = 100 STOPLIGHT_HEIGHT = 250 BUFFER = 75 width=get_width()/2 height=get_height()/2 rect = Rectangle(STOPLIGHT_WIDTH, STOPLIGHT_HEIGHT) rect.set_position(width-50, height-125) rect.set_color(Color.gray) add(rect) def draw_circle(y_pos, color): circ=Circle(LIGHT_RADIUS) circ.set_color(color) circ.set_position(width, y_pos) add(circ) yellow=Color.yellow red=Color.red green=Color.green draw_circle(height, yellow) draw_circle(height-BUFFER, red) draw_circle(height+BUFFER, green)
5.5.5 Min
# Enter your code here def min(num1, num2): if num1>num2: return num2 else: return num1 x=min(6, 10) print("The min is " + str(x)) x=min(26, 50) print("The min is " + str(x)) x=min(1, -1) print("The min is " + str(x))
5.4.5 Quadruple with Return Values
# Enter your code here def quadruple(num): x=num*4 return(x) x=quadruple(7) print(x) x=quadruple(3) print(x) x=quadruple(9) print(x)
5.4.4 Square with Return Values
# Enter your code here def square(num): x=num*num return(x) x=square(5) print (x) x=square(8) print (x) x=square(4) print (x)
5.2.4 Area of Triangle
# Enter your code here def triangle_area(BASE, HEIGHT): area = 1/2 * BASE * HEIGHT print(area) triangle_area(5, 4) triangle_area(10,1) triangle_area(6,2)
5.1.4 Square
# Enter your code here num=int(input("What's your number? ")) def square(x): square_x = x*x print(square_x) square(num) #other calls to test out function square(12) square(1) square(4)
5.2.5 Height in Meters
INCHES_TO_CM = 2.54 CM_TO_METERS = 0.01 FEET_TO_INCHES = 12 # Enter your code here def convert_height_to_meters(feet, inches): feet_in_meter = feet * FEET_TO_INCHES * INCHES_TO_CM * CM_TO_METERS inches_in_meter = inches* INCHES_TO_CM * CM_TO_METERS meter = feet_in_meter + inches_in_meter print(str(feet) + " feet, " + str(inches) +" inches = " + str(meter) + " meters" ) convert_height_to_meters(6, 4) convert_height_to_meters(5, 8) convert_height_to_meters(5, 2)
5.5.4 Is It Even?
SENTINEL = 0 def is_even(num): if num%2==0: return True if num%2==1: return False while True: num=int(input("Enter a number: ")) if(SENTINEL!=num): if(is_even(num)==True): print("Even") elif(is_even(num)==False): print("Odd") elif(SENTINEL==num): break print("Done!")
5.3.4 Horizontal Lines
# Write a function to draw a horizontal # line given a y position and a length def horizontal_line(y, length): line = Line(0 , y, length, y) line.set_color(Color.black) add(line) horizontal_line(100, 200) horizontal_line(200, 100) horizontal_line(300, 20)
5.3.6 Pool Table
POOL_BALL_RADIUS = 40 FONT_TYPE = "30pt Arial" # Write a function called draw_pool_ball that draws a pool ball. def draw_pool_ball(color, num, x, y): circle= Circle(POOL_BALL_RADIUS) circle.set_position(x, y) circle.set_color(color) add(circle) txt=Text(num) txt.set_position(x-10,y+20) txt.set_color(Color.white) txt.set_font("30pt Arial") add(txt) draw_pool_ball(Color.orange, 5, 100, 100) draw_pool_ball(Color.red, 3, 150, 350) draw_pool_ball(Color.blue, 2, 250, 140) draw_pool_ball(Color.green, 6, 50, 200)