CodeHS Python | Unit 5

Information Technology19 CardsCreated 5 months ago

This deck covers key exercises and concepts from CodeHS Python Unit 5, including functions, variables, and programming tasks.

5.1.4: Square | CodeHS

# Enter your code here
def square(x):
double_x = 5 * x
print(double_x)
square(5)

y = 10
square(y)

Tap or swipe ↕ to flip
Swipe ←→Navigate
1/19

Key Terms

Term
Definition

5.1.4: Square | CodeHS

# Enter your code here
def square(x):
double_x = 5 * x
print(double_x)
square(5)

y = 10
square(y)

5.1.5: Triple | CodeHS

# Enter your code here
def triple(x):
triple_x = 3 * x
print(triple_x)
triple(4)
triple(5)

y = 3
triple(y)

5.2.4: Area of Triangle | CodeHS

# Enter your code here
def triangle_area(BASE, HEIGHT):
AREA = 1/2 BASE HEIGHT
print(AREA)
triangle_area(5, 4)
tria...

5.2.5: Height in Meters | CodeHS

INCHES_TO_CM = 2.54
CM_TO_METERS = 0.01
FEET_TO_INCHES = 12
def convert_height_to_meters(feet, inches):
cal_1= feet FEET_TO_INCHES...

5.3.4: Horizontal Lines | CodeHS

# 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, ...

5.3.5: Graphics Stop Light | CodeHS

LIGHT_RADIUS, STOPLIGHT_WIDTH, STOPLIGHT_HEIGHT, BUFFER = 25, 100, 250, 75

width, height = get_width()/2, get_height()/2

rect = Rectang...

Related Flashcard Decks

Study Tips

  • Press F to enter focus mode for distraction-free studying
  • Review cards regularly to improve retention
  • Try to recall the answer before flipping the card
  • Share this deck with friends to study together
TermDefinition

5.1.4: Square | CodeHS

# Enter your code here
def square(x):
double_x = 5 * x
print(double_x)
square(5)

y = 10
square(y)

5.1.5: Triple | CodeHS

# Enter your code here
def triple(x):
triple_x = 3 * x
print(triple_x)
triple(4)
triple(5)

y = 3
triple(y)

5.2.4: Area of Triangle | CodeHS

# 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.2.5: Height in Meters | CodeHS

INCHES_TO_CM = 2.54
CM_TO_METERS = 0.01
FEET_TO_INCHES = 12
def convert_height_to_meters(feet, inches):
cal_1= feet FEET_TO_INCHES
cal_2= cal_1 + inches
cal_3= cal_2
INCHES_TO_CM
cal_4= cal_3 * CM_TO_METERS
print(cal_4)
convert_height_to_meters(6, 4)
convert_height_to_meters(5, 8)
convert_height_to_meters(5, 2)

5.3.4: Horizontal Lines | CodeHS

# 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.5: Graphics Stop Light | CodeHS

LIGHT_RADIUS, STOPLIGHT_WIDTH, STOPLIGHT_HEIGHT, BUFFER = 25, 100, 250, 75

width, height = get_width()/2, get_height()/2

rect = Rectangle(STOPLIGHT_WIDTH, STOPLIGHT_HEIGHT)

rect.set_position(width - STOPLIGHT_WIDTH/2, height - STOPLIGHT_HEIGHT/2)

rect.set_color(Color.GRAY); add(rect)

def draw_circle(y, c): circ = Circle(LIGHT_RADIUS); circ.set_color(c); circ.set_position(width, y); add(circ)

draw_circle(height - BUFFER, Color.RED)

draw_circle(height, Color.YELLOW)

draw_circle(height + BUFFER, Color.GREEN)

5.3.6: Pool Table | CodeHS

POOL_BALL_RADIUS = 40

FONT_TYPE = "30pt Arial"

def draw_pool_ball(color, number, x, y):

circle = Circle(POOL_BALL_RADIUS)

circle.set_position(x, y)

circle.set_color(color)

add(circle)

txt = Text(str(number), FONT_TYPE)

txt.set_position(x - 10, y + 10)

txt.set_color(Color.white)

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)

5.4.4: Square with Return Values | CodeHS

# 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.4.5: Quadruple with Return Values | CodeHS

# 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.5.4: Is It Even? | CodeHS

SENTINEL = 0

def is_even(n): return n % 2 == 0

while True:

num = int(input("Enter a number: "))

if num == SENTINEL: break

print("Even" if is_even(num) else "Odd")

print("Done!")

5.5.5: minVal | CodeHS

# Enter your code here
def minVal(num1, num2):
if num1>num2:
return num2
else:
return num1
x = minVal(6, 10)
print("The min is " + str(x))
x = minVal(26, 50)
print("The min is " + str(x))
x = minVal(1, -1)
print("The min is " + str(x))

5.6.4: Local Variables | CodeHS

def add_one(x): return x + 1

def sum(x, y): return x + y

x = add_one(8); print(x)

y = add_one(10); print(y)

a = sum(10, 20); print(a)

5.7.5: Temperature Converter | CodeHS

def C2F(C): F = 1.8 * C + 32; print(F); return F

def F2C(F): C = (F - 32) / 1.8; print(C); return C

C2F(0); C2F(100)

F2C(40); F2C(80)

5.7.6: Temperature Converter, Part 2 | CodeHS

try: C = float(input("Celsius: ")); print(1.8*C+32)

except ValueError: print("Must be a number.")

try: F = float(input("Fahrenheit: ")); print((F-32)/1.8)

except ValueError: print("Must be a number.")

5.8.4: Making Karel Turn Right | CodeHS

import math; set_size(275,275); A=68; K=Image("https://codehs.com/uploads/9657058ec012105e0c5548c917c29761"); K.set_size(A,A); add(K); D=0

def left(): global D; D=(D+math.pi/2)% (2*math.pi); K.set_rotation(D)

def right(): [left() for _ in range(3)]

def key(e): c=chr(e.charCode);

if c in 'rd': right()

elif c in 'la': left()

add_key_press_handler(key)

5.8.5: Making Karel Move | CodeHS

import math

set_size(275,275); A=68; X=0; Y=275-A; D=0

K=Image("https://codehs.com/uploads/9657058ec012105e0c5548c917c29761"); K.set_size(A,A); add(K)

def move(): global X,Y; X+=A*math.cos(D); Y-=A*math.sin(D); K.set_position(X,Y)

def left(): global D; D=(D+math.pi/2)% (2*math.pi); K.set_rotation(D)

def right(): [left() for _ in range(3)]

def square(): [move(),move(),left(),move(),right(),move()]

timer.set_timeout(move,1000); timer.set_timeout(left,2000); timer.set_timeout(move,3000)

Sorry about Ghosts | CodeHS

I had never got the answer to it, again I apologize for the inconvenience.

5.9.2: Guessing Game | CodeHS

import random

name = input("Hello, what is your name? ")

number = random.randint(1, 100)

print("Hi " + name + ", I'm thinking of a number between 1 and 100.")

guesses = 0

while guesses < 5:

guess = int(input("Enter a guess: "))

guesses += 1

if guess < number:

print("That was too low.")

elif guess > number:

print("That was too high.")

else:

print("You win, " + name + "! You guessed the right number!")

break

else:

print("You lost! Better luck next time.")

Sorry about Draw Something | CodeHS

I had never got the answer to it, again I apologize for the inconvenience.