Function Assignment
Function Assignment
Define the function isLeapYear(year) which takes the year from the user (outside the
function) and pass the year as the parameter. Define this function and check whether it is
leap year or not( inside the function).
def isLeapYear(a):
if (a%4==0 and a%100!=0 or a%400==0):
print("The Year is Leap Year")
else:
print("Not a Leap Year")
a=int(input("Enter a year"))
isLeapYear(a)
---------------------------------------------------------------------------------------------------------------------
Q2. Define the function isLeapYear(year) which takes the year from the user (outside the
function) and pass the year as the parameter. Define this function and take one variable
FLAG which stores Boolean value True or False based on whether the year is Leap year or
not. Print the year is leap or not outside the function based on the returned value of func-
tion isLeapYear(year)
def isLeapYear(a):
if (a%4==0 and a%100!=0 or a%400==0):
print(FLAG)
else:
print("False")
a=int(input("Enter a year"))
FLAG = bool(a)
isLeapYear(a)
---------------------------------------------------------------------------------------------------------------------
Q3. Implement Function and Solve it using parameter passing technique followed in Py-
thon. Consider the price of an item (Biscuit Pack) in a retail store:
(a) Item price of a Biscuit pack is initially Rs. 10
(b) Price is increased by Rs. 25
Expected O/P:
Price before calling : 10
Price after calling ( Inside Function) : 35
Price after calling ( Outside Function) : 10
def price(a):
a=35
print('Price after calling :',a)
a=10
print('Price before calling :',a)
price(a)
print('Price after calling :',a)
---------------------------------------------------------------------------------------------------------------------
Q4. Create a list of Mobile Phones(L) from user. Write the functions to update the existing
list and add new mobiles in the retail store.
def updateList(L):
pass #Define it
def addList(L):
pass #Define it
Expected Output:
If list is L=[“Samsung”,”Nokia”]
Q5. WAP to define the function isprime(n) which uses parameter passing and return val-
ue.
def Isprime(n):
if (n==1):
return False
elif (n==2):
return True;
else:
for a in range(2,n):
if(n % a == 0):
return False
return True
n=int(input('Enter a no.'))
print(Isprime(n))
---------------------------------------------------------------------------------------------------------------------
Q6. WAP which uses random module. Throw the dice 5 times for two players
Expected Output:
Player 1 throw the dice : 5
Player 2 throw the dice: 2
Player 1 score : 18
Player 2 score : 23
Player 2 wins
import random
min = 1
max = 6
s1=0
s2=0
N=int(input("Enter the number of dice you want to roll :"))
for i in range(1,N+1):
a=random.randint(min,max)
print ('Player 1 throw the dice :',a)
b=random.randint(min,max)
print ('Player 2 throw the dice :',b)
s1=s1+a
s2=s2+b
print('Player 1 score :',s1)
print('Player 2 score :',s2)
if s1>s2:
print('Player 1 wins')
else:
print('Player 2 wins')
---------------------------------------------------------------------------------------------------------------------
Submitted By:-
Piyush Srivastava
Section B
Roll No. 08