CH5 ALGORITHM
Look at the following function definition: def my_function(a, b, c): d = (a + c) / b print(d) a. Write a statement that calls this function and uses keyword arguments to pass 2 into a, 4 into b, and 6 into c. b. What value will be displayed when the function call executes?
ANSWER: a. my_function(2,6,4) def my_function(a,c,b): d = (a + c) / b b. 2.0
Examine the following function header, and then write a statement that calls the function, passing 12 as an argument. def show_value(quantity):
ANSWER: sum_value(12):
Write a while loop that lets the user enter a number. The number should be multiplied by 10, and the result assigned to a variable named product. The loop should iterate as long as product is less than 100
CODE: product =1 while product < 100 : packages= int(input('Enter number of packages: ')) product = packages * 10 print('Total in stock:',product) PRINTS: Enter number of packages: 5 Total in stock: 50 Enter number of packages: 10 Total in stock: 100
Rewrite the following statements using augmented assignment operators. a. x = x + 1 b. x = x * 2 c. x = x / 10 d. x = x - 100
a. x + = 1 b. x * = 2 c. x / = 10 d. x - = 100
Write a loop that calculates the total of the following series of numbers: 1/30+2/29+3/28+⋯30/1
# Display a total for number in range sum=0.0 for number in range(1,31): sum += number/(31-number) print('1/30 + 2/29 + 3/28 +...30/1=',sum) PRINTS: 1/30 + 2/29 + 3/28 +...30/1= 93.84460105853213
Look at the following function header: def my_function(a, b, c): Now look at the following call to my_function: my_function(3, 2, 1) When this call executes What value will be assigned to a? What value will be assigned to b? What value will be assigned to c?
3 passed to a 2 passed to b 1 passed to c
Write a function named get_first_name that asks the user to enter his or her first name, and returns it.
ANSWER: def get_first_name(): first_name=input('Enter your first name: ') return(first_name)
The following statement calls a function named half, which returns a value that is half that of the argument. (Assume the number variable references a float value.) Write code for the function: result = half (number)
ANSWER: def half (number): return number/2
Write a function named times_ ten that accepts a number as an argument. When the function is called, it should return the value of its argument multiplied times 10.
ANSWER: def times_ten(number): return (number) *10
Write a statement that generates a random number in the range of 1 through 100 and assigns it to a variable named rand.
ANSWER: rand = random.randint(1,100)
A program contains the following function definition: def cube (num) : return num * num * num Write a statement that passes the value 4 to this function and assigns its return value to the variable result.
ANSWER: result=cube(4)
What will the following program display? def main(): x = 1 y = 3.4 print (x, y) change_us (x, y) print (x, y) def change_us(a, b): a = 0 b = 0 print(a, b) main()
ANSWER: 1 3.4 0 0 1 3.4
Write a function named times_ten. The function should accept an argument and display the product of its argument * 10.
def times_ten(number): product=number*10 print(product)