Chapter 9
Chapter 9 - User Defined Functions
[Solutions for the book "Cs with Python"
by Sumita Arora for class 11]
Page 277
Q1) Write a function that takes amount in dollars and dollars to rupee conversion price; it then returns the amount converted to rupees. Create the function in both void and non-void forms.
Solution :
def nonvoiddtor(x,y): #Nonvoid
res=x*y
return res
x=input("Enter the conversion price ")
y=input("Enter amount to be converted ($$) ")
print "The prince in Rupee is : ", voiddtor(x,y)
def voiddtor(x,y): #Void
res=x*y
print "The price converted in Rupee is : ",res
x=input("Enter the conversion price ")
y=input("Enter amount to be converted ($$) ")
nonvoiddtor(x,y)
Q2) Write a function to calculate volume of a box with appropriate default values for its parameters. Your function should have the following input parameters: (a) length of box; (b) width of box; (c)height of box.
Test it by writing complete program to invoke it.
Solution :
def boxvolume(l,b,h):
l=20.0
b=25.5
h=40.0
res=l*b*h
print res," is the volume of box. "
x=input("Enter length : ")
y=input("Enter breadth : ")
z=input("Enter height : ")
boxvolume(x,y,z)
Q3) Write a program to have following functions :
(i) a function that takes a number as argument and calculates cube for it. The function does not return a value. If there is no value passed to the function in function call, the function should calculate the cube of 2.
(ii) a function that takes two char arguments and return True if both the arguments are equal otherwise False.
Test bot these function by giving appropriate function call statements.
Solution :
def cuberoot(x=2):
print x**3, "is the value "
x=input("Enter the no. to calculate cube ")
cuberoot(x)
Part (ii)
def charcomp(a,b):
if a==b:
print "True"
else:
print "False"
x=raw_input("Enter a character ")
y=raw_input("Enter second character ")
charcomp(x,y)
Ques 4) Write a function that receives two numbers and generates a random number from that range. Using this function, the main program should be able to print three numbers randomly.
Solution :
import random
def myfunc(a,b):
c=random.randint(a,b)
print a,c,b
x=input("Enter a number ")
y=input("Enter 2nd number ")
myfunc(x,y)
Ques 5) Write a function that receives two string arguments and checks wether they are same length strings (returns True in this case otherwise false)
Solution :
def stringcomp(a,b):
if len(a)==len(b):
print "True"
else:
print "False"
x=raw_input("Enter a string ")
y=raw_input("Enter another string ")
stringcomp(x,y)
Ques 6) Write a function namely nthRoot() that receives two parameters x and n and returns the nth root of x. I.e x^1/n
Solution :
def nthRoot(x,n):
res=x**(1.0/n)
print res
a=input("Enter x ")
b=input("Enter n ")
nthRoot(a,b)
Ques 7) Write a function that takes a number n and then returns a randomly generated number hacing exactly n digits(not starting with zero) e.g. if n is 2 then function can randomly return a number 10-99 but 07,02 etc are not valid 2 digit no.s
Solution :
import random
def myfunc(x):
start=int('1'+'0'*(x-1))
print start
stop=int('9'*x)
print stop
print random.randrange(start,stop+1)
a=input("Enter any no. ")
myfunc(a)
[OR]
from random import randint
def randomdigits(n):
rangestart=10**(n-1)
rangeend=(10**n)-1
print randint(rangestart,rangeend)
x=input("Enter a digit ")
randomdigits(x)
Ques 8) Write a function that takes two numbers and returns the number that has minimum one's digit.
Solution :
def onesplace(a,b):
lastdigita=a%10
lastdigitb=b%10
if lastdigita
print a
else:
print b
x=input("Enter any number ")
y=input("Enter another number ")
onesplace(x,y)
----xx----
Thq sir it's really helps us😊
ReplyDeleteSir I think first one is wrong..sorry if i am wrong..
ReplyDelete