Python : User Defined Functions

Write a function that takes a number and prints its square root.

def square_root( ): num=int(input("Enter a number:")) ans=num**0.5 print(" Square root is ",ans) square_root( )

Creates a function that takes two floating point numbers and displays their average.

def average_of_two_numbers( ): a=float(input("Enter first number: ")) b=float(input("Enter second number: ")) ans=(a+b)/2 print( "Average of two numbers is" , ans) average_of_two_numbers( )

Write a function that takes your favourite color as an argument and print it in a sentence.

def favourite_color( a ): print ( " My favourite color is ",a) c = input( " Enter favourite color : ") favourite_color( c )
Create a function that takes a word and print it in uppercase.
def word_in_uppercase( ): w = input ( " Enter a word : ") ans=w.upper( ) print ( " Given word in uppercase : " , ans) word_in_uppercase( )
Write a function that accepts a boolean value and prints whether a person is eligible for voting.
def eligible_for_voting(wants_to_vote): if wants_to_vote: age = int(input("What is your age? ")) if age >= 18: print("You are eligible for voting") else: print("You are not eligible for voting") else: print("Thank you") eligible_for_voting(True)
Create a function that checks whether a shop is open or closed based on a boolean argument.
def shop_is_open_close(is_open): if is_open: print (" Shop is open") else: print("Shop is closed") shop_is_open_close( True ) shop_is_open_close( False )
Write a function that takes a tuple of three city names and prints them one by one .
def show_cities(cities): for city in cities: print(city) cities=("Kathmandu","Butwal","Gorkha") show_cities(cities)
Create a function that accepts a tuple of ages and displays the youngest one
def youngest_one(ages): y=min(ages) print ("Age of yougest one is " , y) ages=(10,12,45,12,5) youngest_one(ages)
Write a function that takes a dictionary containing a book's title and author. Then prints them.
def book_info(book): print ("Book's title is ",book["title"]) print ("Book's Author is ",book["author"]) book={ } book["title"]=input("Enter book name: ") book["author"]=input("Enter book author: ") book_info(book)
Create a function that accepts a dictionary with subject names and marks and print each subject with its marks.
def scoresheet(score): print(score) score={"Nep":60,"Eng":69,"Maths":71,"Sci":70,"Social":64} scoresheet(score)
Write a function that takes your birth year as a variable and calculates your age.
def calculate_age(by): from datetime import datetime cy=datetime.now( ).year a=cy-by print( "Your current age is ",a) by=int(input("Enter your birth year :")) calculate_age(by)
Create a function that accepts the number of hours you study daily and prints a motivational message.
def motivational_message(hour): if hour<2: print(" Increase your study hours") elif hour<4: print("Continue your study ") else: print(" Good , keep it up") hour=int(input("Enter your study hour:")) motivational_message(hour)
Write a function that accepts the result of 5*4 as an argument and displays the result.
def result(a): print("Result is ",a) result(5*4)
Create a function that takes the sum of two numbers as an argument and prints whether it is even or odd.
def test(s): if (s%2==0): print( " EVEN") else: print( "ODD") a=int(input("Enter first number : ")) b=int(input("Enter second number : ")) s=a+b test(s)
Write a function get_city( ) that returns a city name, and another function display_city( ) that takes this value and displays it.
def get_city( ): def display_city(c): return c display_city(c) print("City name is ",c) c=input( " Enter a city name :") get_city( )
Create a function calculate_area( ) that returns area of sqaure , and another function show_area( ) that takes this value and displays it.
def calculate_area(l ): a=l**2 return a def show_area(a): print("area is ",a) l=int(input( " Enter length :")) a=calculate_area( l ) show_area(a)
Back