Compsci

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/10

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

11 Terms

1
New cards

What is a parameter?

A parameter is a function definition for incoming values. 

2
New cards

Why are parameters useful?

They are useful because it makes it easier to code so you don’t have to write as many functions and it also makes it easier to organize the code and be able to alter it later. 

3
New cards

Consider the following two functions, which return the area of a circle with a radius of 5 and a radius of 10, respectively.
The issue is that we would have to write a function for every possible radius. Solve this problem by writing a function with a parameter.

def areaOfCircle(radius):

return 3.14 * radius + radius

4
New cards

Make a list of numbers with 4 elements

numbers = [1, 2, 3, 4]

5
New cards

Use a for loop to traverse the list, printing every element in it

for number in numbers:

print (number)

6
New cards

How do lists manage complexity in a program? (hint: too many variables)

Lists manage complexity in programs so you don’t have to write a new variable for every value. It also helps by organizing different variables in different lists making it easier and clearer to code. 

7
New cards

What is the distinction between defining and calling a function?

The difference between defining and calling a function is that defining it is creating it and defining what it does. When you call a function it is making it execute and use the function. 

8
New cards

Write a function that calculates the average of two numbers, it should have two parameters, num1 and num2:
def average(num1, num2): or function average(num1, num2){}

it should not print the average, but instead return it.

def average (num1, num2):

sum = num1 + num2

average = sum/2

return average

9
New cards

Show a call to the function, storing the return result in a variable

result = average (5,6) 

10
New cards

Assume we have a list with variable name “accessories”. Write an algorithm that counts how many times “cat” occurs in the list.

accessories = [“hat”, “scarf”, “cat”, “cat”, “earrings”]

catCount = 0 

for accessory in accessories:

if accessory == “cat”:

catCount = catCount +1

11
New cards

AP score question

APscores = [5,3,4,2]

sum = 0

for score in APscores:

sum = sum + score