Accounting /CodeHS Python | Unit 5

CodeHS Python | Unit 5

Accounting19 CardsCreated 3 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
SSpeak
FFocus
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

# This program should draw a stop light
LIGHT_RADIUS = 25
STOPLIGHT_WIDTH = 100
STOPLIGHT_HEIGHT = 250
BUFFER = 75
# Implement a fun...

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

# This program should draw a stop light
LIGHT_RADIUS = 25
STOPLIGHT_WIDTH = 100
STOPLIGHT_HEIGHT = 250
BUFFER = 75
# Implement a function that draws a single circle
# with radius LIGHT_RADIUS.
# The circle should be in the center of the screen horizontally.
# Use the parameters for the y position and color
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.3.6: Pool Table | CodeHS

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, x, y):
circle = Circle(POOL_BALL_RADIUS)
circle.set_position(x, y)
circle.set_color(color)
add(circle)

txt = Text
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)

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(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.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

# All you have to do in this exercise is write a good comment that explains
# what local variables are. You should also give an example of a function
# and what the local variables are, and what the scope is of the variable.
# The local variables are variables that are declared inside of a function or loop. Since they are declared inside of the function, they belong only to that one function and no other functions.
# scope of a variable is its lifetime in the program. The scope is variable the block of code in the entire program where the variable is declared, used, and can be modified.
def add_one(x):
# The result variable in this function is totally
# different than the one in the sum function
result = x + 1
return result
def sum(x, y):
result = x + y
return result

x = add_one(8)
print(x)
y = add_one(10)
print(y)
a = sum(10, 20)
print(a)

5.7.5: Temperature Converter | CodeHS

# Write your function for converting Celsius to Fahrenheit here.
# Make sure to include a comment at the top that says what
# each function does!
def C2F(C):
Fahre = F = (1.8 * C) + 32
print(Fahre)
return(Fahre)
def F2C(F):
Cels = C = (F - 32) / 1.8
print(Cels)
return(Cels)

# Now write your function for converting Fahrenheit to Celsius.
# Now change 0C to F:
C2F(0)
# Change 100C to F:
C2F(100)
# Change 40F to C:
F2C(40)
# Change 80F to C:
F2C(80)

5.7.6: Temperature Converter, Part 2 | CodeHS

try:
C = int(input("whats your number in celsius "))
def to_f (C):
value1 = float((1.8 * C) + 32)
print(value1)
to_f (C)
except ValueError:
print("must be a number.")


try:
F = int(input("whats your number in Fahrenheit "))
def to_c (F):
value_2 = float((F - 32)/8)
print (value_2)
v
except ValueError:
print("must be a number")

5.8.4: Making Karel Turn Right | CodeHS

# This program creates Karel without any of the builtin Karel commands,
# it makes Karel from scratch using Python.
#
# In this program, we build Karel's World in Python.
NUM_STREETS = 4
NUM_AVES = 4
POINT_SIZE = 3
WORLD_WIDTH = 275
WORLD_HEIGHT = 275
set_size(WORLD_WIDTH, WORLD_HEIGHT)
STREET_HEIGHT = WORLD_HEIGHT // NUM_STREETS
AVE_WIDTH = WORLD_WIDTH // NUM_AVES
PAUSE_TIME = 1000
# Constants for creating Karel
KAREL_IMG_URL = "https://codehs.com/uploads/9657058ec012105e0c5548c917c29761"
KAREL_SIZE = STREET_HEIGHT
# Starting position for Karel
X_POS = 0
Y_POS = WORLD_HEIGHT - KAREL_SIZE
# represents angles of rotation
EAST = 0
SOUTH = math.radians(90)
WEST = math.radians(180)
NORTH = math.radians(270)
# Creates Karel's world with Karel in the bottom left corner facing east.
def setup_world():
global direction, karel
# Add the points to the grid
for street in range(NUM_STREETS):
for ave in range(NUM_AVES):
x_center = ave AVE_WIDTH + AVE_WIDTH / 2
y_center = street
STREET_HEIGHT + STREET_HEIGHT / 2
dot = Circle(POINT_SIZE, x_center, y_center)
add(dot)

# Add Karel to the grid
karel = Image(KAREL_IMG_URL)
karel.set_position(X_POS, Y_POS)
karel.set_size(KAREL_SIZE, KAREL_SIZE)
add(karel)
# Variables to keep track of karel and karel's direction
# Set Karel's initial direction
direction = EAST
def turn_left():
global karel, direction

if direction == EAST:
direction = NORTH
elif direction == WEST:
direction = SOUTH
elif direction == NORTH:
direction = WEST
elif direction == SOUTH:
direction = EAST
else:
print("Error: Karel's Direction is not properly set.")
direction = EAST

karel.set_rotation(direction)

# This function turns Karel to the right

def turn_right():
turn_left()
turn_left()
turn_left()
# Press 'r' or 'd' to make Karel turn right
# Press 'l' or 'a' to make Karel turn left
# Make sure to click on Karel's world first
def rotate_karel(e):
ch = chr(e.charCode)
if ch 'r' or ch 'd':
turn_right()
elif ch 'l' or ch 'a':
turn_left()

add_key_press_handler(rotate_karel)
setup_world()

5.8.5: Making Karel Move | CodeHS

# This program creates Karel without any of the builtin Karel commands,
# it makes Karel from scratch using Python.
#
# In this program, we build Karel's World in Python.
NUM_STREETS = 4
NUM_AVES = 4
POINT_SIZE = 3
WORLD_WIDTH = 275
WORLD_HEIGHT = 275
set_size(WORLD_WIDTH, WORLD_HEIGHT)
STREET_HEIGHT = WORLD_HEIGHT // NUM_STREETS
AVE_WIDTH = WORLD_WIDTH // NUM_AVES
PAUSE_TIME = 1000
# Constants for creating Karel
KAREL_IMG_URL = "https://codehs.com/uploads/9657058ec012105e0c5548c917c29761"
KAREL_SIZE = STREET_HEIGHT
# Starting position for Karel
X_POS = 0
Y_POS = WORLD_HEIGHT - KAREL_SIZE
# represents angles of rotation
EAST = 0
SOUTH = math.radians(90)
WEST = math.radians(180)
NORTH = math.radians(270)
# Creates Karel's world with Karel in the bottom left corner facing east.
def setup_world():
global direction, karel
# Add the points to the grid
for street in range(NUM_STREETS):
for ave in range(NUM_AVES):
x_center = ave AVE_WIDTH + AVE_WIDTH / 2
y_center = street
STREET_HEIGHT + STREET_HEIGHT / 2
dot = Circle(POINT_SIZE, x_center, y_center)
add(dot)

# Add Karel to the grid
karel = Image(KAREL_IMG_URL)
karel.set_position(X_POS, Y_POS)
karel.set_size(KAREL_SIZE, KAREL_SIZE)
add(karel)
# Variables to keep track of karel and karel's direction
# Set Karel's initial direction
direction = EAST
def turn_left():
global karel, direction

if direction == EAST:
direction = NORTH
elif direction == WEST:
direction = SOUTH
elif direction == NORTH:
direction = WEST
elif direction == SOUTH:
direction = EAST
else:
print("Error: Karel's Direction is not properly set.")
direction = EAST

karel.set_rotation(direction)

# Copy your code from the last exercise
def turn_right():
pass
# Implement this function!
def move():
global X_POS, Y_POS
# Put your code here
# X_POS and Y_POS stores Karel's coordinates
setup_world()
# Moves Karel in a square
timer.set_timeout(move, PAUSE_TIME)
timer.set_timeout(move, PAUSE_TIME*2)
timer.set_timeout(turn_left, PAUSE_TIME*3)
timer.set_timeout(move, PAUSE_TIME*4)
timer.set_timeout(turn_right, PAUSE_TIME*5)
timer.set_timeout(move, PAUSE_TIME*6)
timer.set_timeout(turn_right, PAUSE_TIME*7)
timer.set_timeout(move, PAUSE_TIME*8)

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
# Enter your code here
name = input("Hello, What is you name? ")
number = random.randint(1, 100)
print("Hi " + name + ", I'm thinking of a number between 1 and 100.")
guessesTaken = 0
while guessesTaken < 5:
guess = input("Enter a guess: ")
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print("That was too low.")
elif guess > number:
print("That was too high.")
else:
break
if guess == number:
print("You win " + name + " You guessed the right number!")
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.