DEV Community

Suresh S
Suresh S

Posted on • Updated on

Python Functions - Solved

Task after functions class was checked. 7 tasks were given.. all the tasks are compelted.

Lessons learnt:

  • If comparison should always be with string with '' (if choice ==1 is incorrect but choice=='1' is correct.. choice is received as an input from user
  • print should not be assigned with any value
  • function definition must be on top not in below
  • Recursive function is not clear
  • Found a way to summarise multiple function task in single code.
  • Should not do IO like prints inside the function unnecessarily which is one of the good practice
  • Python has wonderful organization of code by itself with indent option
  • return type of function is dynamic. The operation inside the function determines the return type. eg, div, argument received may be int but final value will be float.

Tasks:

  1. Write a function greet that takes a name as an argument and prints a greeting message.
  2. Write a function sum_two that takes two numbers as arguments and returns their sum.
  3. Write a function is_even that takes a number as an argument and returns True if the number is even, and False if it is odd.
  4. Write a function find_max that takes two numbers as arguments and returns the larger one.
  5. Write a function multiplication_table that takes a number n and prints the multiplication table for n from 1 to 10.
  6. Write a function celsius_to_fahrenheit that takes a temperature in Celsius and returns the temperature in Fahrenheit.
  7. Write a function power that takes two arguments, a number and an exponent, and returns the number raised to the given exponent. The exponent should have a default value of 2.
import time

###########################################################################################################################
# 1. Write a function greet that takes a name as an argument and prints a greeting message
###########################################################################################################################
def greet(name):
    print(f"Hello {name}! Welcome to my python code. \nThank you for attending this Task party\n")

# greet('Suresh')
# greet(input("Welcome Guest: "))

###########################################################################################################################
# 2. Write a function sum_two that takes two numbers as arguments and returns their sum.
###########################################################################################################################
def sum_two(num1, num2):
    result = num1+num2
    print(f"Addition of {num1} and {num2} is: {result}\n")
    return result

# add = sum_two(3, 5)
# add=sum_two(int(input("Enter the num1 for addition: ")), int(input("Enter the num2 for addition: ")))

#############################################################################################################################
# 3. Write a function is_even that takes a number as an argument and returns True if the number is even, and False if it is odd.
#############################################################################################################################

def is_even(num):
    if num % 2 == 0:
        print (f"The number {num} is Even")
        return True

    else:
        print (f"The number {num} is Odd")
        return False

print(is_even (4))
print(is_even (int(input("Enter the number to find Odd/Even: "))))

# num = int(input("Enter the number to find Odd/Even: "))
# print = is_even(num)

###########################################################################################################################
#  4. Write a function find_max that takes two numbers as arguments and returns the larger one.
###########################################################################################################################

def find_max(num1, num2):
    if num1>num2:
        print (f"The number {num1} is bigger")
        return num1

    elif num1<num2:
        print (f"The number {num2} is bigger")
        return num2

    else:
        print(f"Both the numbers {num1} and {num2} are equal")

# result = find_max(int(input("Enter the num1 to find max: ")), int(input("Enter the num2 to find max: ")))
# print("\n")

###########################################################################################################################
#  5. Write a function multiplication_table that takes a number n and prints the multiplication table for n from 1 to 10.
###########################################################################################################################
def multiplication_table(n):
    for i in range(1,11):
        print(f"{i} x {n} = {i*n}")
        time.sleep(0.5)

# multiplication_table(int(input("Enter the multiplication table required: ")))
# print("\n")

###########################################################################################################################
# 6. Write a function celsius_to_fahrenheit that takes a temperature in Celsius and returns the temperature in Fahrenheit.
###########################################################################################################################
def celsius_to_fahrenheit(celsius):
    fahrenheit = (celsius * 9/5) + 32
    print(f"{celsius}°C is equal to {fahrenheit}°F")
    return fahrenheit


###########################################################################################################################
# 7. Write a function power that takes two arguments, a number and an exponent, and returns the number raised to the given exponent. 
# The exponent should have a default value of 2.
###########################################################################################################################
def power(base, exponent=2):
    result = base ** exponent
    print(f"The {base} raised to the power of {exponent} is {result}")
    return result


def tasks_function():
    # print("Tasks from function")
    print("Select Task")
    print("1. Function to greet and printing greeting message ")
    print("2. Function to sum two numbers")
    print("3. Function to find odd or even")
    print("4. Bigger number from two inputs")
    print("5. Multiplication table")
    print("6. celcius to Fahrenheit")
    print("7. Exponent of number ")

    # choice = input("Enter choice(1/2/3/4/5/6/7): ")

    total_times = 3
    while(total_times>0):
        total_times = total_times-1
        choice = input("Enter choice(1/2/3/4/5/6/7): ")

        if choice=='1':
            greet(input("Welcome Guest: "))

        elif choice=='2':
            add=sum_two(int(input("Enter the num1 for addition: ")), int(input("Enter the num2 for addition: ")))

        elif choice =='3':
            num = int(input("Enter the number to find Odd/Even: "))
            is_even(num)

        elif choice=='4':
            result = find_max(int(input("Enter the num1 to find max: ")), int(input("Enter the num2 to find max: ")))
            print("\n")

        elif choice=='5':
            multiplication_table(int(input("Enter the multiplication table required: ")))
            print("\n")

        elif choice=='6':
            celsius = int(input("Enter the temperature in Celsius: "))
            celsius_to_fahrenheit(celsius)

        elif choice=='7':
            base = int(input("Enter the base number: "))
            exponent_input = input("Enter the exponent (default is 2): ")
            if exponent_input:
                exponent=int(exponent_input)
            else:
                exponent=2
            # exponent = int(exponent_input) if exponent_input else 2
            power(base, exponent)

        else:
            print("Invalid Choice. Please enter a number between 1 and 7.")

tasks_function()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)