functions that illustrate modules random and time
# Area of a circle
def area(radius):
import math
return math.pi * radius ** 2
# Deal a hand of n cards
def dealHand(n):
import random # Import the functions from module random
deck = range(1, 53) # A fresh deck is cards 1..52 (where 1..13 are spades, etc.)
random.shuffle(deck) # Shuffle the deck. I.e. randomize order of list elements
return deck[0 : n] # Return the first n of the shuffled deck
# Roll n (six-sided) dice
def rollDice(n):
import random
total = 0
for die in range(n):
roll = int((random.random() * 6) + 1)
total = total + roll
return total
# What day is it?
def todaysDay():
import time # Import the functions from module time
now = time.ctime() # Get the current time and date in string format
return now[0:3]
# What year is it?
def thisYear():
import time # Import the functions from module time
now = time.ctime() # Get the current time and date in string format
return now[-4:] # Find last four digits. That's the year
Link to this Page