MENU

Chapter 10 Functions Solutions

Question - 71 : -
How to generate random numbers in Python ?

Answer - 71 : -

The standard module random implements a radom number generator. *тАЩ
There are also many other in this module, such as :
uniform(a, b) returns a floating point number in the range [a, b].
randint(a, b) returns a random integer number in the range [a, bj.
random() returns a floating point number in the range [0,1].
Following code snippet show usage of all the different functions, evertime it is executed.
import random
i = random.randint (1, 99) # i randomly initialized by integer between range 1 and 99.
j = random randient (1,99) # j randomly initialized by float between range 1 and 99.
k = random.random() # k randomly initialized by float between range 0 and 1

Print (тАЬi.тАЭ, i)
Print (тАЬj.тАЭ,j)
Print (тАЬk.тАЭ, k)
_______________
Output:
(тАШi:тАЩ, 64)
(тАШj:тАЩ 701.85008797642115)
(тАШk:тАЩ: 0.18173593240301023)
Output:
(тАШi:тАЩ,83)
(тАШj:тАЩ,56.817584548210945)
(тАШk:тАЩ; 0.9946957743038618)

Question - 72 : -
Define a function тАШSubtract Number (x, y)тАЩ which takes in two numbers and returns the different of the two.

Answer - 72 : -

# Python function which returns the difference of two numbers
> > > a = input (тАЬEnter the first number :тАЭ)
> > > b = input (тАЬEnter the second number :тАЭ) def Subtract Number (a, b):
> > > print тАЬSubtraction = %d тАУ %dтАЭ % (a, b)
> > > return a тАУ b

Question - 73 : -
Write a program that takes a number and calculate and display the log, square, sin and cosine of it.

Answer - 73 : -

# Program to display the various values of a number import math
> > > number = input (тАЬEnter the number :тАЭ)
> > > print тАЬNumber isтАЭ, number
> > > print тАЬLog value of number isтАЭ math.log (number)
> > > print тАЬSquare of number isтАЭ, math.pow (number, 2)
> > > print тАЬSin value of number isтАЭ, math.sin (number)
> > > print тАЬCosine value of number isтАЭ, math.cos (number)

Question - 74 : -
Write a tic-tac-toe program in python.

Answer - 74 : -

# tic-tac-toe program
import random def drawboard (board):
# This function is used to print the board
print (тАШ | | тАШ)
print (тАШ тАШ + board [7] + тАШ j тАШ + board [8]
+ тАШ | тАШ + board [9]) print (тАШ| | тАШ)
print (тАШ тАШ)
print (тАШ| | тАШ)
print (тАШ тАШ + board [4] + тАШ | тАШ + board [5]
+ тАШ | тАШ + board [6]) print (тАШ| | тАШ)
print (тАШ тАШ)
Print (тАШ | | тАШ)
print (тАШ тАШ + board [1] + тАШ | тАШ + board [2]
+ тАШ | тАШ + board [3]) print (тАШ| | тАШ)
def input player letter ():
# This function is used to display the user typed letter
Letter = тАЭ
while not (letter = = тАШXтАЩ or letter = = тАШOтАЩ):
print (тАШDo you want to be X or O ?тАЩ)
letter = input () .upper ()
if letter = тАШXтАЩ return [тАШXтАЩ, тАШOтАЩ]
else:
return [тАШOтАЩ, тАШXтАЩ] def whogoesFirst ():
# This function is used to randomly choose
the player
if random . randint (0,1) = = 0 : return тАШComputerтАЩ else:
return тАШPlayerтАЩ def Play Again ():
# This function is used when player wants play again print
(тАШDo you want to play again ? (Yes or no)тАЩ) return input (), lower ().startswith (тАШyтАЩ) def makeMove (board, letter, move): board[move] = letter def is Winner (bo, le):
# This function returns true when plays has
won return

(60[7] = = le and 60[8] = = le and 60[9] = = le) or
(60[4] = = le and 60[5] = = le and 60[6] = = le) or
(60[1 ] = = le and 60[2] = = le and 60[3] = = le) or
(60[7] = = le and 60[4] = = le and 60[1] = = le) or
(60[8] = = le and 60[5] = = le and 60[2] = = le) or
(60[9] = = le and 60[6] = = le and 60[3] = = le) or
(60[7] = = le and 60[5] = = le and 60[3] = = le) or
(60[9] = = le and 60[5] = = le and 60[1] = = le) or

def getBoardcopy(board):
# This function make a duplicate of the board dupeBoard = [ ]
for i in board :
dupeBoard.append(i)
return depeBoard
def isSpaceFree (board, move):
# This function returns Ttue if the passed
move is free return boardfmove] = = тАШ тАШ def getPlayerMove(board):
# This function is used to take playerтАЩs move
move = тАШ тАШ
while move not inтАЩ12345678 9тАЩ.split () or
not is spaceFree (board, int(move)):
print (тАШWhat is your next move ? (1-9)тАЩ)
move = input () return int(move)
def chooseRandomMoveFromList(board,
movelist):
# This function returns a valid move from the passed list possible Moves = [ ]
for i in moveList:
if is SpaceFree (board, i):
possibleMOves.append(i)
if len(possibleMoves)! = 0 :
return random.choice (possibleMoves)
else:
return тАУ None
def getComputerMove (board, computerletter):
# This function returns the computerтАЩs letter
if computer letter = = тАШXтАЩ:
player letter = тАШOтАЩ
else:
player letter = тАШXтАЩ
for i in range (1,10):
copy = getBoardCopy (board)
if isSpaceFree (copy, i):
makeMove (copy, computerletter, i)
if is Winner (copy, computerletter):
return i
for i in range (1,10):
copy = getBoard Copy (board)
if isSpaceFree (copy, i):
makeMove (copy, player letter, i)
if is Winner (copy, player letter):
return i
move = chooseRandomMoveFromList (board, [1, 3, 7, 9])
if move ! = None :
return move
if isSpaceFree (board, 5):
return 5
return ChooseRandomMoveFromList (board, [2,4,6,8])
def is BoardFull (board):
# This function returns true if all space on board has been taken
for i in range (1,10):
if isSpaceFree (board, i):
return False
return True
print (тАЬWelcome to Tic-Tac-Toe !тАЭ)
while True:
the Board = [тАЭ * 10
playerletter, computerletter = input
playerletter()
turn = whoGoesFirst()
print (тАШTheтАЩ + turn + тАШwill go first.тАЩ)
gamelsPlaying = True
while gemelsPlaying:
if turn = = тАШplayerтАЩ:
drawBoard (theBoard)
move = getPlayerMove (theBoard)
makeMove (theBoard, playerletter, move)
if is Winner (theBoard, playletter):
drawBoard (theBoard)
print (тАШHooray ! You have won !тАЩ)
gamelsPlaying = False
esle:
if is BoardFull (theBoard):
drawBoard (theBoard)
print (тАШThe game is a tie.тАЭ)
break
else:
turn = тАШcomputerтАЩ
else:
move=getCompterMove (theBoard, Computerletter)
makeMove (theBoard, computer letter, move)
if is Winner (theBoard, computerletter): drawBoard (theBoard)
print(тАШThe computer has beaten you!тАЩ) gamelsPlaying = False
else:
if isBoardFull (theBoard):
drawBoard (theBoard)
print (тАШThe game is a tie !тАЩ)
break
else:
turn = тАШplayerтАЩ if not play Again ():
break

Free - Previous Years Question Papers
Any questions? Ask us!
×