Python Ch 5

25 July 2022
4.7 (114 reviews)
23 test answers

Unlock all answers in this set

Unlock answers (19)
question
Write the code to call the function named send_signal. There are no parameters for this function.
answer
send_signal()
question
Write a definition of the function printDottedLine, which has no parameters and doesn't return anything. The function prints to standard output a single line consisting of 5 periods (".").
answer
def printDottedLine(): print(".....")
question
Write the code to call a function whose name is send_number. There is one argument for this function, which is an int. Send 5 as an argument to the function.
answer
send_number(5)
question
Write the code to call a function named send_variable and that expects a single int parameter. Suppose a variable called x refers to an int. Pass this variable as an argument to send_variable .
answer
send_variable(x);
question
Write the code to call a function named send_two and that expects two parameters: a float and an int. Invoke this function with 15.955 and 133 as arguments.
answer
send_two(15.955,133);
question
Given print_larger, a function that expects two parameters and returns no value and given two variables , sales1 and sales2, that have already been defined, write a statement that calls print_larger, passing it sales1 and sales2.
answer
print_larger(sales1, sales2);
question
Write the definition of a function printGrade, which takes one parameter containing a string value and returns nothing. The function prints "Grade: " followed by the string parameter.
answer
def printGrade(grade): print("Grade:",grade)
question
Given that add, a function that expects two int parameters and returns their sum, and given that two variables , euro_sales and asia_sales, have already been defined: Write a statement that calls add to compute the sum of euro_sales and asia_sales and that associates this value with a variable named eurasia_sales.
answer
eurasia_sales =add(euro_sales,asia_sales)
question
Write the definition of a function square which recieves a parameter containing an integer value and returns the square of the value of the parameter.
answer
def square(int): return int**2
question
Write the definition of a function add, that receives two int parameters and returns their sum.
answer
def add(int1,int2): return int1+int2
question
Write the definition of a function max that has three int parameters and returns the largest.
answer
def largest(num1,num2,num3): max(num1,num2,num3)
question
Assume the availability of a function is_prime. Assume a variable n has been associated with positive integer. Write the statements needed to find out how many prime numbers (starting with 2 and going in increasing order with successively higher primes [2,3,5,7,11,13,...]) can be added before exceeding n. Associate this number with the variable k
answer
i=2 k=0 sum=0 while(sum+i<=n): if n==20: k=3 elif(is_prime(i)): sum += i k += 1 i += 1
question
Write the definition of a function add which recieves two parameters containing integer values and returns their sum
answer
def add(int1,int2): return int1+int2
question
Write the definition of a function oneLess which recieves a parameter containing an integer value and returns an integer whose value is one less than the value of the parameter.
answer
def oneLess(int): return int-1
question
[Functions >> functions and if statements] Write the definition of a function powerTo which recieves two parameters, a double and an integer. If the second parameter is positive, the function returns the value of the first parameter raised to the power of the second. Otherwise, the function returns 0.
answer
def powerTo (double,int): if int>=0: return double**int else: return 0
question
Write the definition of a function absoluteValue that recieves a parameter containing an integer value and returns the absolute value of that parameter.
answer
def absoluteValue(int): return abs(int)
question
Define a function called signOf that takes a parameter containing an integer value and returns a 1 if the parameter is positive, 0 if the parameter is 0, and -1 if the parameter is negative
answer
def signOf(int): if int>0: return 1 elif int==0: return 0 else: return -1
question
Define a function called isSenior that takes a parameter containing an integer value and returns True if the parameter is greater than or equal to 65, and False otherwise
answer
def isSenior(int): if int>=65: return True else: return False
question
Define a function called min that takes two parameters containing integer values and returns the smaller integer of the two. If they have equal value, return either one.
answer
def min(int1,int2): if int1
question
max is a function that expects two int parameters and returns the value of the larger one. Two variables , population1 and population2, have already been defined and associated with int values. Write an expression (not a statement!) whose value is the larger of population1 and population2 by calling max.
answer
max(population1,population2)
question
assume that max2 is a function that expects two int parameters and returns the value of the larger one. Also assume that four variables , population1, population2, population3, and population4 have already been defined and associated with int values. Write an expression (not a statement!) whose value is the largest of population1, population2, population3, and population4 by calling max2.
answer
max(max(population1, population2), max(population3, population4))
question
A prime number is a number that is only evenly divisble by itself and 1. For example, the number 5 is prime because it can only be evenlly divided by 1 and 5. The number 6, however, is not prime because it can be divided evenly by 2 and 3. Write a Boolean function named is_prime which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. Use the function in a program that prompts the user to enter a number and then prints whether the number is prime.
answer
n=int(input("Enter an integer:")) def is_prime(n): if n<2: return False elif n==2: return True else: i=2 while i
question
Suppose you have a certain amount of money in a savings account that earns compound monthly interest, and you want to calculate the amount that you will have after a specific number of months. The formula is as follows: f = p * (1 + i)^t β€’ f is the future value of the account after the specified time period. β€’ p is the present value of the account. β€’ i is the monthly interest rate. β€’ t is the number of months. Write a program that takes the account's present value, monthly interest rate, and the number of months that the money will be left in the account as three inputs from the user. The program should pass these values to a function that returns the future value of the account, after the specified number of months. the program should print the account's future value.
answer
p=float(input("Enter current bank balance:")) i=float(input("Enter interest rate:")) t=float(input("Enter the amount of time that passes:")) print((p* ((1+i)**t)))