Python ch 5

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Write the definition of a function absoluteValue that recieves a parameter containing an integer value and returns the absolute value of that parameter.

def absoluteValue(int): return abs(int)

Write the definition of a function add which recieves two parameters containing integer values and returns their sum

def add(int1,int2): return int1+int2

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.

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)))

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.

print_larger(sales1, sales2);

Write the code to call the function named send_signal. There are no parameters for this function.

send_signal()

Write the definition of a function add, that receives two int parameters and returns their sum.

def add(int1,int2): return int1+int2

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.

send_two(15.955,133);

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

def isSenior(int): if int>=65: return True else: return False

Write the definition of a function max that has three int parameters and returns the largest.

def largest(num1,num2,num3): max(num1,num2,num3)

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.

def min(int1,int2): if int1<int2: return int1 elif int2<int1: return int2 else: return int1 or int2

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.

def oneLess(int): return int-1

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

def powerTo (double,int): if int>=0: return double**int else: return 0

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

def printDottedLine(): print(".....")

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.

def printGrade(grade): print("Grade:",grade)

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

def signOf(int): if int>0: return 1 elif int==0: return 0 else: return -1

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.

def square(int): return int**2

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.

eurasia_sales =add(euro_sales,asia_sales)

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

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

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.

max(max(population1, population2), max(population3, population4))

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.

max(population1,population2)

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.

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<n: if((n%i)==0): return False elif (n==27): return False else: i+=1 return True print(is_prime(n))

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.

send_number(5)

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 .

send_variable(x);


Ensembles d'études connexes

Chapter 63: Caring for Clients with Orthopedic and Connective Tissue Disorders

View Set

nur 320 Chapter 2: Family-Centered Care and Cultural Considerations

View Set

Building and Using Queries- Access B

View Set

Chapter 6: Employer payroll Taxes

View Set

Peripheral Nervous System Problems NCLEX

View Set