Q. 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.
(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.
You can understand by Watching video :-
Answer :-
(i)
def cube( a = 2 ) : print( "Cube of ", a ,"=" , a ** 3 ) num = input("Enter a number (For empty press Enter ) :") if num == "" : cube() else : cube( int (num) )
Output
Enter a number (For empty press Enter ) :9
Cube of 9 = 729
>>>
Enter a number (For empty press Enter ) :
Cube of 2 = 8
>>>
(ii)
def chr(char1,char2) : if char1 == char2 : return "True" else: return"False " char1 = input("Enter a Char 1 : ") char2 = input("Enter a Char 2 : ") print(chr(char1,char2))
Output :-
Enter a Char 1 : Path
Enter a Char 2 : Walla
False
>>>
Enter a Char 1 : Path
Enter a Char 2 : Path
True
>>>
print("Ture")
ReplyDelete**(True)
"True"
DeleteIt will give Error
Deletetur
DeleteIf print("True") is written after if statement and if we remove print in last line then it wont give error right?
ReplyDeleteYes, Output is same.
DeleteThankyou sir
ReplyDeleteWelcome : )
DeleteThanks sir
ReplyDeleteWelcome : )
Deleteappreciate dedication for making video for each and every question thank you very much
ReplyDeleteWelcome : )
Deletedef chr(char1,char2):
ReplyDeletereturn(char1,char2)
char1 = input("Enter Char1 :")
char2 = input("Enter Char2 :")
if char1 == char2:
print("True")
else:
print("False")
def cube():
Deleten = input("enter number :")
if n == "":
print("Cube of 2 =",2**3)
else:
print("Cube of",int(n),"=",int(n)**3)
cube()
Your answer of first question is correct but second one is wrong because you are returning char itself.
Deletedef abhi(a , b):
ReplyDeleteif a == b :
return 1
else:
return 2
a = str(input("Enter 2nd Chr:- "))
b= str(input("Enter 1st Chr:- "))
c = abhi(a,b)
if c ==1:
print("True")
else:
print("False")
???
Deletebro think he the riddler
DeleteAre you a software engineer? What is your name ?
ReplyDeletebond. james bond
DeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )