CSCI 131 – Python Language



CSCI 131 – Python Language

Functions Practice

Example:

#function definition

def welcome (name):

print “Hi’, name

print “Welcome to the programming class!”

# main function that uses the function welcome

myName = raw_input ( “Please enter the name “ )

welcome ( myName )

yourName = raw_input(“Please enter the name “ )

welcome ( yourName )

name = raw_input ( “Please enter the name “ )

welcome ( name )

Problem 1:

Write a function divisor (a, b) that determines if b is divisor of a. The function will return 1 if b is divisor of a, and 0 otherwise. Write a program that reads 4 pairs of numbers and check for each pair: if the second number is a divisor of the first number.

Problem 2:

n! ( n factorial ) is defined as follows : n! = 1*2*3*… *n. Write a function factorial (n) that calculates and returns the value n!

Write a program that calculates and prints factorial of the number for all numbers between 1 and 7

(including 1 and 7).

Problem 3:

Write four different functions sum (a, b) that finds a+b, product (a, b) that finds a*b,

remainder (a, b) that finds a%b, division (a, b) that finds a/b.

Write a program that reads 10 pairs of numbers and for each pair calculates sum, product, remainder and quotient of the numbers in the pair.

Problem 4:

Write two functions: average2 (a, b) that funds the average of a and b, and

average3 (a, b, c) that finds the average of am b and c.

Write a program that reads 6 numbers and finds the average of the input numbers using functions average3 and average2.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download

To fulfill the demand for quickly locating and searching documents.

It is intelligent file search solution for home and business.

Literature Lottery

Related searches