Computer science using python
Worksheet with 5 problems we have to solve using python it’s a intro to computer science course.
You may work with another student; if you do, submit one assignment with both names on it.
- Finish the following Python program so that it prints out the numbers in number_list that are divisible by 3. It should work if number_list is changed to any other list of integers, without requiring other changes to the program.
number_list = [1,-4,35,6,9,23,54] for n in number_list: # Write an if statement here to make the program work.
- Finish the following Python program so that it prints a rectangle of * characters, using the values of length and height for the dimensions of the rectangle. For example, with the values of length and height below, the program should print
***** *****
length = 5 height = 2 # Add a for statement here to make the program work.
- Turn the program from question 2 into a function called print_rectangle that has two parameters, length and height. Follow your function definition with some testing.
def print_rectangle(length,height): #Put your code here (probably the same code you added in #question #2.
#Put some statements here to test the function.
- Define a function print_rectangle_that does just what print_rectangle did, but that accepts a third parameter that is a character, so that the function prints a rectangle using that character, not always with ‘*’.
- (More challenging). Based on what you did for question 1, write a fruitful function called multiples_of_3 that accepts one parameter called integer_list. This function should return the number (or “count”) of terms in the list that are multiples of 3. If you do this correctly, the program
print multiples_of_3([11,12,13,14,15,16])
will print the number 2, because there are two multiples of 3
Leave a Reply
Want to join the discussion?Feel free to contribute!