A _______ is a variable that is created inside a function.

A _______ is a variable that is created inside a function.


a. global variable

b. local variable

c. hidden variable

d. none of the above; you cannot create a variable inside a function


Answer: local variable

You created the following dictionary relationships = {'Jimmy':'brother'}. You then executed the following code, and received a KeyError exception. What is the reason for the exception?

You created the following dictionary relationships = {'Jimmy':'brother'}. You then executed the following code, and received a KeyError exception. What is the reason for the exception?


relationships['jimmy']

Jimmy and jimmy are different


What is the value of the variable string after the execution of the following code?


myString = 'abcd'

myString = myString.upper()


Answer: 'ABCD'

A value-returning function is ________.

A value-returning function is ________.


Answer: a function that will return a value back to the part of the program that called it

Which are posttest loops?

Which are posttest loops?


Answer: Do-While and Do-Unit

A value-returning function is _____.

A value-returning function is _____.


Answer: a function that will return a value back to the part of the program that called it

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.

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

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.

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 an expression (not a statement!) whose value is the largest of population1, population2, population3, and population4 by calling max2.

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

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

[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

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

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

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.

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)

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.

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