Chapter 03 - Working With Functions
Chapter 03 - Working With Functions
1. Write a function that takes amount in dollars and dollar to rupees conversion price; it then returns the amount converted to rupees. Create the function in both void and non-void forms.
YOUR CODE HERE
2.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.
YOUR CODE HERE
3. 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 cube of 2.
def CubeOfNumber(user_input=2):
print('Cube of',user_input,'is',user_input**3)
user_input=eval(input("Please insert a number: "))
CubeOfNumber(user_input)
CubeOfNumber()
(ii) a function that takes two char arguments and returns True if both the arguments are equal otherwise False.
Test both these functions by giving appropriate function call statements.
def compare(a,b):
if a==b:
return True
else:
return False
a=input("Enter first character: ")
b=input("Enter second character: ")
compare(a,b)
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.from random import randint
def randomNumber(a,b):
c=randint(a,b)
return c
a=int(input('first number: '))
b=int(input('second number: '))
print('three random numbers between',a,'and',b,'are', randomNumber(a,b),',',randomNumber(a,b),'and',randomNumber(a,b))
5. Write a function that receives two arguments and checks whether they are same-length strings(returns True in this case otherwise false).def checkLength(a,b):
if len(a)==len(b):
return True
else:
return False
a=input('Enter first string: ')
b=input('Enter second string: ')
print(checkLength(a,b))
6. Write a function namely nth Root() that receives two parameters x and n and returns nth root of x i.e., x^(1/n).
The default value of n is 2.
def nthRoot(x,n=2):
x=x**(1/n)
return x
x=int(input('Enter a number: ') )
n=int(input('Enter root value: '))
print(nthRoot(x,n))
print(nthRoot(x))
7. Write a function that takes a number n and then returns a randomly generated number having 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 two digit numbers.
from random import randint
def randomNumber(n):
start='1'
end=' '
for i in range(n-1):
start=start+'0'
for i in range(n):
end=end+'9'
randomNumber=randint(int(start),int(end))
return randomNumber
n=int(input('Enter a number :'))
print(randomNumber(n))
8. Write a function that takes two numbers and returns the number that has minimum one's digit.def compare(a,b):
c=len(a)-1
d=len(b)-1
if int(a[c])>int(b[d]):
return b
else:
return a
a=str(input('Enter 1st number: '))
b=str(input('Enter 2nd number: '))
print(compare(a,b))
9. Write a program that generates a series using a function which h takes first and last values of the series and then generates four terms that are equidistant e.g., if two numbers passed are 1 and 7 then function returns 1 3 5 7.
your question 4 is wrong
ReplyDeletefrom random import randint
def randomNumber(a,b):
c=randint(a,b)
return c
a=int(input('first number: '))
b=int(input('second number: '))
print('three random numbers between',a,'and',b,'are', randomNumber(a,b),',',randomNumber(a,b),'and',randomNumber(a,b))
isma randint ki jagha randrange ayaga in [hind in language]
randrange needs a step value you can use randrange or randint since question only said to produce a random int from a given range
Delete