Pearson My Programing Lab Python lab 7

Ace your homework & exams now with Quizwiz!

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

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

Using the is_prime function from exercise 17, write a program that printsthe prime numbers from 1 to 100 each on their own line.

def is_prime(n): (Tab) if n > 1: (Tab) (Tab) for i in range(2,n): (Tab) (Tab) (Tab) if (n % i) == 0 : (Tab) (Tab) (Tab) (Tab) return False (Tab) (Tab) return True (Tab) else: (Tab) (Tab) return False for i in range (1,101): (Tab) if(is_prime(i)==True): (Tab) print(i)

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 1and 5. The number 6, however, is not prime because it can be divided evenlyby 2 and 3.Write a Boolean function named is_prime which takes an integer as an argumentand returns true if the argument is a prime number, or false otherwise. Usethe function in a program that prompts the user to enter a number and thenprints whether the number is prime.

def is_prime(n): (Tab) if n > 1: (Tab) for i in range(2,n): (Tab) (Tab) if (n % i) == 0 : (Tab) (Tab) (Tab) return False (Tab) return True (Tab) else: (Tab) (Tab) return False n = int(input("Enter an integer:")) print(is_prime(n))

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(): (Tab) 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): (Tab) print("Grade:",grade)

Write the definition of a function twice, that receives an int parameter and returns an int that is twice the value of the parameter.

def twice(int): (Tab) return 2*int

Write the definition of a function typing_speed, that receives two parameters. The first is the number of words that a person has typed (an int greater than or equal to zero) in a particular time interval. The second is the length of the time interval in seconds (an int greater than zero). The function returns the typing speed of that person in words per minute (a float).

def typing_speed(num_words,time_interval): (Tab) num_words>=0 (Tab) time_interval>0 (Tab) result=float(60*num_words/time_interval) (Tab) return result

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

Assume that print_error_description is a function that expects one int parameter and returns no value. Write a statement that invokes the function print_error_description, passing it the value 14.

print_error_description(14)

Assume that print_todays_date is a function that uses no parameters and returns no value. Write a statement that calls (invokes) this function.

print_todays_date()

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)


Related study sets

Chapter 10 procedures, pharmacology and abbreviations

View Set

Chapter 10: Exercise for Health and Fitness

View Set

chapter 14 the importance of warranties

View Set

Ch. 29 (Saving, Investment, and the Financial System)

View Set

Chapter 16 Cost Accounting Cost Allocation: Joint Products and Byproducts

View Set

Cultural Psychology Quiz and R and D Questions

View Set

ENGINE FUEL SYSTEMS (Jepp/Prep/Crane)

View Set