Module Practice
Fill in the blank with the code that best completes the program.
Try to solve them all on paper before you enter your answers in JES.
#(1) The circumference of a circle
def circumference(radius):
import __________ # Not strictly necessary, because this method is imported automatically
return __________ *radius*2 # 2 times pi times r
#(2) Deal a hand of n cards
def dealHand(n):
import _______ # Import the functions from module random
deck = range(1, 53) # A fresh deck is cards 1..52 (where 1..13 are spades, etc.)
______________(deck) # Shuffle the deck. I.e. randomize order of list elements
return deck[0 : n] # Return the first n of the shuffled deck
#(3) Roll n (six-sided) dice
def rollDice():
import ____________ # Import the functions from module random
total = 0 # The total of the n dice
for die in range(3): # Roll each die
roll = int((________________* 6) + 1) # The roll is between 1 and 6 inclusive but must be integer
total = total + roll # Add this roll to the previous ones
return total
#(4) What day is it?
def todaysDay():
import ________ # Import the functions from module time
now = _____________ # Get the current time and date in string format
return now[0:3] # Day is the first three letters of the date string
#(5) What year is it?
def thisYear():
import _________ # Import the functions from module time
now = _______________ # Get the current time and date in string format
return now[-4:] # Find last four digits. That's the year
#(6) Count how many .jpg files in a directory
def countJPG(dir):
import ____ #Not strictly necessary, because this module is imported automatically
file_list = _____________(dir) #Get all of the files in the directory 'dir' and put them into a list
count = 0 #Start count at zero
for file in file_list: #Loop to go through evey file name in the list
if file.endswith('.jpg'): #Check each file name to see if it ends with '.jpg'
count = count + 1 #If it does end in '.jpg' then add 1 to count
return count #Return the accumulated sum of count
Links to this Page