Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

1. Write a function called `greet` that takes a name as a parameter and prints a greeting message.

Test the function with different names.


Solution: Program to Implement the `greet` function that takes a name as a parameter and prints a greeting
message:
def greet(name):
"""
This function takes a name as input and prints a greeting message.
Parameters:
name (str): The name of the person to greet.
"""
print("Hello, " + name + "! How are you today?")

# Test the function with different names


greet("Alice")
greet("Bob")
greet("Charlie")
Explanation: In this program, we have defined a function called `greet` that takes a single parameter `name`.
Inside the function, it prints a greeting message that includes the provided name.
When we call the function `greet` with different names, it will execute the function body and print the
corresponding greeting message. For example, when we call `greet("Alice")`, it will print "Hello, Alice! How are
you today?".
The function parameter `name` acts as a placeholder for the value that will be passed when calling the
function. It allows us to reuse the same function logic for different input values.

Solution: modified implementation of the greet function that takes a name as a parameter using the input
method and prints a greeting message:
def greet():
"""
This function takes a name as input using the `input` method and prints a greeting message.
"""
name = input("Enter your name: ")
print("Hello, " + name + "! How are you today?")

# Test the function


greet()
In this updated program, the greet function doesn't have any parameters. Instead, it uses the input method to
take the name as input from the user. The input method displays the prompt "Enter your name: " and waits for
the user to enter their name. The entered name is then stored in the name variable.
After that, the function prints the greeting message that includes the provided name.
When we call the function greet(), it will execute the function body, prompt the user to enter their name, and
print the greeting message accordingly.
You can test the function multiple times by running the program and entering different names each time.
2. Create a function called `calculate_area` that takes the length and width of a rectangle as parameters
and returns the area of the rectangle. Test the function with different lengths and widths.
Solution: Implementation of the `calculate_area` function that takes the length and width of a rectangle as
parameters and returns the area of the rectangle:
def calculate_area(length, width):
"""
This function calculates the area of a rectangle based on its length and width.

Parameters:
length (float or int): The length of the rectangle.
width (float or int): The width of the rectangle.

Returns:
float or int: The calculated area of the rectangle.
"""
area = length * width
return area

# Test the function with different lengths and widths


length1 = 5
width1 = 10
area1 = calculate_area(length1, width1)
print("Area of Rectangle 1:", area1)

length2 = 7.5
width2 = 3.2
area2 = calculate_area(length2, width2)
print("Area of Rectangle 2:", area2)

length3 = 12
width3 = 12
area3 = calculate_area(length3, width3)
print("Area of Rectangle 3:", area3)

In this program, the `calculate_area` function takes two parameters: `length` and `width`. Inside the function, it
multiplies the length and width to calculate the area of the rectangle. The calculated area is then returned
using the `return` statement.
We test the function by calling it with different lengths and widths (`length1`, `width1`, `length2`, `width2`,
`length3`, `width3`) and store the returned areas in variables (`area1`, `area2`, `area3`). Finally, we print the
areas of the rectangles.
For example, when we call `calculate_area(5, 10)`, it will calculate the area of the rectangle with length 5 and
width 10, which is 50. The variable `area1` will store the value 50, and we print "Area of Rectangle 1: 50".
Solution: Modified implementation of the `calculate_area` function that takes the length and width of a
rectangle as parameters using the `input` method and returns the area of the rectangle:
def calculate_area():
"""
This function takes the length and width of a rectangle as input using the `input` method,
and calculates the area of the rectangle.

Returns:
float: The calculated area of the rectangle.
"""
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))

area = length * width


return area

# Test the function


area1 = calculate_area()
print("Area of Rectangle 1:", area1)

area2 = calculate_area()
print("Area of Rectangle 2:", area2)

area3 = calculate_area()
print("Area of Rectangle 3:", area3)

In this updated program, the `calculate_area` function doesn't have any parameters. Instead, it uses the `input`
method to take the length and width of the rectangle as input from the user. The `input` method prompts the
user to enter the length and width, and the entered values are converted to floats using the `float` function.

After that, the function calculates the area of the rectangle by multiplying the length and width. The calculated
area is then returned using the `return` statement.

When we call the function `calculate_area()`, it will execute the function body, prompt the user to enter the
length and width of the rectangle, calculate the area based on the inputs, and return the result.

You can test the function multiple times by running the program and entering different lengths and widths for
each rectangle.
3. Implement a function called `find_max` that takes a list of numbers as a parameter and returns the
maximum value in the list. Test the function with different lists of numbers.
Solution: Implementation of the `find_max` function that takes a list of numbers as a parameter and returns the
maximum value in the list:

def find_max(numbers):
"""
This function finds the maximum value in a given list of numbers.

Parameters:
numbers (list): A list of numbers.

Returns:
int or float: The maximum value in the list.
"""
max_value = max(numbers)
return max_value

# Test the function with different lists of numbers


numbers1 = [10, 5, 7, 14, 3]
max_value1 = find_max(numbers1)
print("Maximum value in numbers1:", max_value1)

numbers2 = [2.5, 9.8, 4.2, 7.1]


max_value2 = find_max(numbers2)
print("Maximum value in numbers2:", max_value2)

numbers3 = [-3, -10, -5, -2]


max_value3 = find_max(numbers3)
print("Maximum value in numbers3:", max_value3)

In this program, the `find_max` function takes a list of numbers as the `numbers` parameter. Inside the function,
it uses the `max` function to find the maximum value in the given list. The maximum value is then returned using
the `return` statement.

We test the function by calling it with different lists of numbers (`numbers1`, `numbers2`, `numbers3`) and store
the returned maximum values in variables (`max_value1`, `max_value2`, `max_value3`). Finally, we print the
maximum values.

For example, when we call `find_max([10, 5, 7, 14, 3])`, it will find the maximum value in the list [10, 5, 7, 14, 3],
which is 14. The variable `max_value1` will store the value 14, and we print "Maximum value in numbers1: 14".
Solution: Modified implementation of the `find_max` function that takes a list of numbers as a parameter using
the `input` method and returns the maximum value in the list:

def find_max():
"""
This function takes a list of numbers as input using the `input` method,
and finds the maximum value in the list.

Returns:
int or float: The maximum value in the list.
"""
numbers = input("Enter a list of numbers, separated by spaces: ").split()
numbers = [float(num) for num in numbers]

max_value = max(numbers)
return max_value

# Test the function with different lists of numbers


max_value1 = find_max()
print("Maximum value in list 1:", max_value1)

max_value2 = find_max()
print("Maximum value in list 2:", max_value2)

max_value3 = find_max()
print("Maximum value in list 3:", max_value3)
```

In this updated program, the `find_max` function doesn't have any parameters. Instead, it uses the `input` method
to take a list of numbers as input from the user. The user is prompted to enter a list of numbers, separated by
spaces. The input is then split using the `split` method to create a list of strings.

Next, the list of strings is converted to a list of floats using a list comprehension. This ensures that the elements
in the list are treated as numbers rather than strings.

After that, the function finds the maximum value in the list using the `max` function. The maximum value is then
returned using the `return` statement.

When we call the function `find_max()`, it will execute the function body, prompt the user to enter a list of
numbers, convert the input into a list of floats, find the maximum value in the list, and return the result.

You can test the function multiple times by running the program and entering different lists of numbers each time.
4. Write a function called `is_even` that takes an integer as a parameter and returns `True` if the number
is even and `False` otherwise. Test the function with different numbers.
Solutions: Implementation of the `is_even` function that takes an integer as a parameter and returns `True` if
the number is even, and `False` otherwise:

def is_even(number):
"""
This function checks if a given number is even.

Parameters:
number (int): An integer number.

Returns:
bool: True if the number is even, False otherwise.
"""
if number % 2 == 0:
return True
else:
return False

# Test the function with different numbers


print(is_even(10)) # True
print(is_even(7)) # False
print(is_even(0)) # True
print(is_even(-5)) # False

In this program, the `is_even` function takes an integer number as the `number` parameter. Inside the function,
it checks if the number is divisible by 2 using the modulo operator `%`. If the remainder is 0, it means the number
is even, and the function returns `True`. Otherwise, if the remainder is not 0, it means the number is odd, and
the function returns `False`.

We test the function by calling it with different numbers and print the results.

For example, when we call `is_even(10)`, it will check if 10 is even. Since 10 is divisible by 2 without any
remainder, the function will return `True`, and we will print `True` as the result. Similarly, for `is_even(7)`, the
function will return `False` because 7 is not divisible by 2.
Solutions: Modified implementation of the `is_even` function that takes an integer as a parameter using the
`input` method and returns `True` if the number is even, and `False` otherwise:

def is_even():
"""
This function takes an integer as input using the `input` method,
and checks if the number is even.

Returns:
bool: True if the number is even, False otherwise.
"""
number = int(input("Enter an integer: "))

if number % 2 == 0:
return True
else:
return False

# Test the function with different numbers


print(is_even()) # Test with user input
print(is_even()) # Test with user input
print(is_even()) # Test with user input
```

In this updated program, the `is_even` function doesn't have any parameters. Instead, it uses the `input` method
to take an integer as input from the user. The user is prompted to enter an integer, and the input is converted to
an integer using the `int` function.

Inside the function, it checks if the number is even by dividing it by 2 and checking if the remainder is 0. If the
remainder is 0, it means the number is even, and the function returns `True`. Otherwise, if the remainder is not
0, it means the number is odd, and the function returns `False`.

When we call the function `is_even()`, it will execute the function body, prompt the user to enter an integer, check
if the number is even, and return the result.

You can test the function multiple times by running the program and entering different integers each time.
5. Create a function called `print_info` that takes a person's name, age, and city as parameters and
prints out their information. Test the function with different sets of information.
Solutions: Implementation of the `print_info` function that takes a person's name, age, and city as parameters
and prints out their information:

def print_info(name, age, city):


"""
This function takes a person's name, age, and city as parameters
and prints out their information.

Parameters:
name (str): The person's name.
age (int): The person's age.
city (str): The person's city of residence.
"""
print("Name:", name)
print("Age:", age)
print("City:", city)

# Test the function with different sets of information


print_info("John Doe", 25, "New York")
print_info("Jane Smith", 30, "London")
print_info("Alex Johnson", 40, "Paris")

In this program, the `print_info` function takes three parameters: `name`, `age`, and `city`. Inside the function, it
simply prints out the provided information using the `print` function.

We test the function by calling it with different sets of information. For example, when we call `print_info("John
Doe", 25, "New York")`, it will print the following output:

Name: John Doe


Age: 25
City: New York

Similarly, when we call `print_info("Jane Smith", 30, "London")`, it will print:


Name: Jane Smith
Age: 30
City: London

And so on for the other test cases.

# Test the function with different sets of information


print_info("John Doe", 25, "New York")
print_info("Jane Smith", 30, "London")
print_info("Alex Johnson", 40, "Paris")
print_info("Emily Brown", 35, "Sydney")
print_info("Michael Davis", 45, "Los Angeles")

By adding these additional test cases, we can see the function in action with different sets of information. Each
time the function is called, it will print out the provided name, age, and city. The output will be displayed in the
console, allowing you to verify that the function is correctly printing the information.

Feel free to add more test cases as needed to further test the functionality of the `print_info` function.

Solutions: Certainly! Here's the modified implementation of the `print_info` function that takes a person's name,
age, and city as parameters using input method and prints out their information:

def print_info():
"""
This function takes a person's name, age, and city as input using the input method
and prints out their information.
"""
name = input("Enter the person's name: ")
age = int(input("Enter the person's age: "))
city = input("Enter the person's city: ")

print("Name:", name)
print("Age:", age)
print("City:", city)

# Test the function with different sets of information


print("Enter information for Person 1:")
print_info()

print("Enter information for Person 2:")


print_info()

print("Enter information for Person 3:")


print_info()

In this program, the `print_info` function does not take any parameters. Instead, it prompts the user to enter the
person's name, age, and city using the `input` function. The name is stored in the `name` variable, the age is
converted to an integer using `int(input(...))`, and the city is stored in the `city` variable.

After gathering the information, the function then prints out the provided information using the `print` function.
We test the function by calling it multiple times for different sets of information. Each time the function is called,
it will prompt the user to enter the details for a specific person and then print out the information.

For example, running the program and entering "John Doe" for the name, 25 for the age, and "New York" for the
city will produce the following output:

Enter information for Person 1:


Enter the person's name: John Doe
Enter the person's age: 25
Enter the person's city: New York
Name: John Doe
Age: 25
City: New York

Similarly, you can test the function with different sets of information by running the program and providing the
necessary input when prompted.
6. Implement a function called `calculate_average` that takes a list of numbers as a parameter and
returns the average value. Test the function with different lists of numbers.
Solutions: An implementation of the `calculate_average` function that takes a list of numbers as a parameter
and returns the average value:

def calculate_average(numbers):
"""
This function takes a list of numbers as input and returns the average value.
"""
if len(numbers) == 0:
return 0 # Handle empty list case

total = sum(numbers)
average = total / len(numbers)
return average

# Test the function with different lists of numbers


numbers1 = [1, 2, 3, 4, 5]
print("Average:", calculate_average(numbers1))

numbers2 = [10, 20, 30, 40, 50]


print("Average:", calculate_average(numbers2))

numbers3 = [2.5, 5.7, 8.9, 3.2, 6.4]


print("Average:", calculate_average(numbers3))

In this program, the `calculate_average` function takes a list of numbers as the `numbers` parameter. It first
checks if the list is empty using `len(numbers) == 0`, and if so, it returns `0` to handle the case when the list is
empty.

If the list is not empty, the function calculates the total sum of the numbers in the list using the `sum` function
and divides it by the length of the list to obtain the average.

We test the function by creating different lists of numbers and passing them as arguments to the
`calculate_average` function. The calculated average is then printed to the console.

For example, running the program with `numbers1 = [1, 2, 3, 4, 5]` will output:

Average: 3.0

Similarly, you can test the function with different lists of numbers by creating new lists and passing them as
arguments to the `calculate_average` function.
Solutions: Implementation of the `calculate_average` function that takes a list of numbers as a parameter using
the `input` function, calculates the average value, and returns it:

def calculate_average():
"""
This function takes a list of numbers as input using the `input` function
and returns the average value.
"""
numbers = input("Enter a list of numbers separated by spaces: ").split()
numbers = [float(num) for num in numbers] # Convert input values to float

if len(numbers) == 0:
return 0 # Handle empty list case

total = sum(numbers)
average = total / len(numbers)
return average

# Test the function with different lists of numbers


print("Average:", calculate_average())

In this program, the `calculate_average` function doesn't take any parameters. Instead, it uses the `input`
function to prompt the user to enter a list of numbers separated by spaces. The input string is then split using
the `split` method, which returns a list of strings.

Next, a list comprehension is used to convert each string element in the `numbers` list to a float using
`float(num)`. This ensures that the input values are treated as numbers.

If the resulting `numbers` list is empty, the function returns `0` to handle the case when the list is empty.

Otherwise, the function calculates the total sum of the numbers in the list using the `sum` function and divides it
by the length of the list to obtain the average.

The average value is then returned by the function and printed to the console using the `print` function.

When you run the program, it will prompt you to enter a list of numbers. For example, you can input `1 2 3 4 5`
and press Enter. The function will calculate the average and print it as follows:

Enter a list of numbers separated by spaces: 1 2 3 4 5


Average: 3.0

You can test the function with different lists of numbers by providing different input values when prompted. The
function will calculate the average and display it accordingly.
7. Write a function called `reverse_string` that takes a string as a parameter and returns the reversed
version of the string. Test the function with different strings.
Solutions: Implementation of the `reverse_string` function that takes a string as a parameter and returns the
reversed version of the string:

def reverse_string(string):
"""
This function takes a string as input and returns the reversed version of the string.
"""
return string[::-1]

# Test the function with different strings


print(reverse_string("Hello")) # Output: olleH
print(reverse_string("Python")) # Output: nohtyP
print(reverse_string("12345")) # Output: 54321

In this program, the `reverse_string` function takes a `string` parameter. It uses string slicing with a step value
of `-1` (`[::-1]`) to reverse the string. This slicing syntax returns a new string that starts from the last character
and goes backwards until the first character.

The function then returns the reversed string.

We test the function by calling it with different strings and printing the results using the `print` function. In the
example above, we test the function with the strings "Hello", "Python", and "12345", and the function returns the
reversed versions of these strings.

When you run the program, the reversed strings will be printed as follows:

olleH
nohtyP
54321

You can test the function with different strings by providing different input values as arguments when calling the
function. The function will return the reversed version of the input string.
Solutions: Implementation of the `reverse_string` function that takes a string as a parameter using the `input`
method and returns the reversed version of the string:

def reverse_string():
"""
This function takes a string as input using the `input` method
and returns the reversed version of the string.
"""
string = input("Enter a string: ")
return string[::-1]

# Test the function


reversed_str = reverse_string()
print("Reversed string:", reversed_str)

In this program, the `reverse_string` function doesn't take any parameters explicitly. Instead, it uses the `input`
method to prompt the user to enter a string. The input string is then stored in the `string` variable.

The function uses string slicing with a step value of `-1` (`[::-1]`) to reverse the string. This slicing syntax returns
a new string that starts from the last character and goes backwards until the first character.

Finally, the function returns the reversed string.

We test the function by calling it and assigning the returned reversed string to the variable `reversed_str`. We
then print the reversed string using the `print` function.

When you run the program, it will prompt you to enter a string. After entering the string, the program will return
and print the reversed version of the entered string.

You can test the function with different strings by entering different values when prompted. The function will
return the reversed version of the input string.
8. Create a function called `multiply_elements` that takes a list of numbers as a parameter and returns
the product of all the numbers. Test the function with different lists of numbers.
Solutions: Implementation of the `multiply_elements` function that takes a list of numbers as a parameter and
returns the product of all the numbers:

def multiply_elements(numbers):
"""
This function takes a list of numbers as input and returns
the product of all the numbers.
"""
result = 1
for num in numbers:
result *= num
return result

# Test the function


numbers_list = [2, 3, 4, 5]
product = multiply_elements(numbers_list)
print("Product:", product)

In this program, the `multiply_elements` function takes a single parameter `numbers`, which is expected to be a
list of numbers.

The function initializes a variable `result` to `1` to store the running product of the numbers.

It then iterates over each number in the `numbers` list using a `for` loop. For each number, it multiplies it with
the current value of `result` and updates the `result` variable.

Finally, the function returns the computed product.

We test the function by creating a list of numbers called `numbers_list` containing `[2, 3, 4, 5]`. We then call the
`multiply_elements` function with `numbers_list` as the argument and assign the returned product to the variable
`product`.

Finally, we print the product using the `print` function.

You can test the function with different lists of numbers by modifying the `numbers_list` variable with different
sets of numbers. The function will return the product of all the numbers in the list.
Solutions: Implementation of the `multiply_elements` function that takes a list of numbers as a parameter using
the input method and returns the product of all the numbers:

def multiply_elements():
"""
This function takes a list of numbers as input and returns
the product of all the numbers.
"""
numbers = input("Enter a list of numbers (separated by spaces): ")
numbers_list = [float(num) for num in numbers.split()]

result = 1
for num in numbers_list:
result *= num
return result

# Test the function


product = multiply_elements()
print("Product:", product)

In this program, the `multiply_elements` function doesn't take any parameters explicitly. Instead, it uses the
`input` function to prompt the user to enter a list of numbers. The input is expected to be entered with spaces
separating each number.

The function then processes the input by splitting the string into individual numbers using the `split` method and
converting each number to a float using a list comprehension.

After that, it initializes a variable `result` to `1` to store the running product of the numbers.

It then iterates over each number in the `numbers_list` using a `for` loop. For each number, it multiplies it with
the current value of `result` and updates the `result` variable.

Finally, the function returns the computed product.

We test the function by calling it without any arguments and assigning the returned product to the variable
`product`.

Finally, we print the product using the `print` function.

You can test the function with different lists of numbers by entering them when prompted by the input statement.
The function will return the product of all the numbers in the list.
9. Implement a function called `convert_temperature` that takes a temperature in Celsius as a parameter
and returns the temperature in Fahrenheit. Test the function with different temperatures.
Solutions: Implementation of the `convert_temperature` function that takes a temperature in Celsius as a
parameter and returns the temperature in Fahrenheit:

def convert_temperature(celsius):
"""
This function takes a temperature in Celsius as input and
returns the temperature converted to Fahrenheit.
"""
fahrenheit = (celsius * 9/5) + 32
return fahrenheit

# Test the function


celsius_temp = 25
fahrenheit_temp = convert_temperature(celsius_temp)
print("Temperature in Fahrenheit:", fahrenheit_temp)

In this program, the `convert_temperature` function takes a single parameter `celsius`, which represents the
temperature in Celsius.

Inside the function, it applies the formula `(celsius * 9/5) + 32` to convert the temperature from Celsius to
Fahrenheit.

The calculated temperature in Fahrenheit is then returned from the function.

To test the function, we assign a value of 25 to the variable `celsius_temp` to represent a temperature in Celsius.

We then call the `convert_temperature` function with `celsius_temp` as the argument and assign the returned
Fahrenheit temperature to the variable `fahrenheit_temp`.

Finally, we print the converted temperature using the `print` function.

You can test the function with different temperatures by assigning different values to `celsius_temp` variable.
The function will return the temperature converted to Fahrenheit.
Solutions: Implementation of the `convert_temperature` function that takes a temperature in Celsius as a
parameter and returns the temperature in Fahrenheit:

def convert_temperature(celsius):
"""
This function takes a temperature in Celsius as input and
returns the temperature converted to Fahrenheit.
"""
fahrenheit = (celsius * 9/5) + 32
return fahrenheit

# Test the function


celsius_temp = float(input("Enter a temperature in Celsius: "))
fahrenheit_temp = convert_temperature(celsius_temp)
print("Temperature in Fahrenheit:", fahrenheit_temp)

In this program, the `convert_temperature` function takes a single parameter `celsius`, which represents the
temperature in Celsius.

Inside the function, it applies the formula `(celsius * 9/5) + 32` to convert the temperature from Celsius to
Fahrenheit.

The calculated temperature in Fahrenheit is then returned from the function.

To test the function, we use the `input` function to prompt the user to enter a temperature in Celsius. The input
is expected to be a float.

We then call the `convert_temperature` function with the entered temperature as the argument and assign the
returned Fahrenheit temperature to the variable `fahrenheit_temp`.

Finally, we print the converted temperature using the `print` function.

You can test the function with different temperatures by entering them when prompted by the input statement.
The function will return the temperature converted to Fahrenheit.
10. Write a function called `is_palindrome` that takes a string as a parameter and returns `True` if the
string is a palindrome (reads the same forwards and backwards), and `False` otherwise. Test the
function with different strings.
Solutions: Implementation of the `is_palindrome` function that takes a string as a parameter and returns `True`
if the string is a palindrome, and `False` otherwise:

def is_palindrome(string):
"""
This function takes a string as input and checks if it is a palindrome.
Returns True if the string is a palindrome, False otherwise.
"""
reversed_string = string[::-1] # Reverse the string using slicing
if string == reversed_string:
return True
else:
return False

# Test the function


test_string1 = "level"
print(test_string1, "is a palindrome:", is_palindrome(test_string1))

test_string2 = "python"
print(test_string2, "is a palindrome:", is_palindrome(test_string2))

In this program, the `is_palindrome` function takes a single parameter `string`, which represents the input string.

Inside the function, it creates a reversed version of the string by using slicing with a step of -1. This effectively
reverses the characters in the string.

Then, it compares the original string with the reversed string using an `if` statement. If they are the same, it
returns `True`, indicating that the string is a palindrome. Otherwise, it returns `False`.

To test the function, we call it with different strings and print the result using the `print` function.

In the example above, we test the function with two strings: "level" and "python". The function correctly identifies
that "level" is a palindrome, while "python" is not.
Solutions: Implementation of the `is_palindrome` function that takes a string as a parameter using the `input()`
method and returns `True` if the string is a palindrome, and `False` otherwise:

def is_palindrome():
"""
This function takes a string as input from the user and checks if it is a palindrome.
Returns True if the string is a palindrome, False otherwise.
"""
string = input("Enter a string: ")
reversed_string = string[::-1] # Reverse the string using slicing
if string == reversed_string:
return True
else:
return False

# Test the function


print(is_palindrome())

In this program, the `is_palindrome` function doesn't take any parameters. Instead, it prompts the user to enter
a string using the `input()` function.

Inside the function, it creates a reversed version of the string by using slicing with a step of -1. This effectively
reverses the characters in the string.

Then, it compares the original string with the reversed string using an `if` statement. If they are the same, it
returns `True`, indicating that the string is a palindrome. Otherwise, it returns `False`.

To test the function, we simply call it using `print(is_palindrome())`. The user is prompted to enter a string, and
the function checks if it's a palindrome and returns the result.
11. Design a function called `print_pattern` that takes a character and a number as parameters and
prints a pattern using the given character. For example, if the character is '#' and the number is 5,
the function should print:
#
##
###
####
#####
Test the function with different characters and numbers.
Solutions: Implementation of the `print_pattern` function that takes a character and a number as parameters
and prints a pattern using the given character:

def print_pattern(character, number):


"""
This function takes a character and a number as input and prints a pattern using the given character.
"""
for i in range(1, number + 1):
print(character * i)

# Test the function


print_pattern('#', 5)
print_pattern('*', 3)
print_pattern('$', 7)

In this program, the `print_pattern` function takes two parameters: `character` and `number`.

Inside the function, it uses a `for` loop to iterate from 1 to the given `number`. On each iteration, it multiplies
the `character` by the current iteration number and prints the resulting string.

To test the function, we call it multiple times with different characters and numbers. In the example above, we
test the function with the character '#' and the number 5, '*', and 3, and '$' and 7.

This will produce the following output:


#
##
###
####
#####
*
**
***
$
$$
$$$
$$$$
$$$$$
$$$$$$$

The function allows you to print patterns of any character and any desired length.

Solutions: Implementation of the `print_pattern` function that takes a character and a number as parameters
using the input method and prints a pattern using the given character:

def print_pattern():
"""
This function takes a character and a number as input and prints a pattern using the given character.
"""
character = input("Enter a character: ")
number = int(input("Enter a number: "))

for i in range(1, number + 1):


print(character * i)

# Test the function


print_pattern()

In this program, the `print_pattern` function doesn't take any parameters explicitly. Instead, it prompts the user
to enter the character and number using the `input` function.

Inside the function, it uses a `for` loop to iterate from 1 to the given `number`. On each iteration, it multiplies
the `character` by the current iteration number and prints the resulting string.

To test the function, we call it once after defining it. The function will prompt the user to enter the character and
number, and then it will print the pattern using the entered values.

For example, if the user enters '#' as the character and 5 as the number, the function will print the following
pattern:

#
##
###
####
#####

You can test the function with different characters and numbers by calling it multiple times.
12. Write a function called `calculate_discount` that takes the original price and a discount
percentage as parameters and returns the discounted price. Test the function with different original
prices and discount percentages.
Solutions: Implementation of the `calculate_discount` function that takes the original price and a discount
percentage as parameters and returns the discounted price without using an input method:

def calculate_discount(original_price, discount_percentage):


"""
This function takes the original price and a discount percentage as input
and returns the discounted price.
"""
discount = original_price * (discount_percentage / 100)
discounted_price = original_price - discount
return discounted_price

# Test the function


original_price = 100 # Example original price
discount_percentage = 20 # Example discount percentage

discounted_price = calculate_discount(original_price, discount_percentage)


print("Discounted price:", discounted_price)

In this program, the `calculate_discount` function takes the original price and discount percentage as parameters.
It calculates the discount amount by multiplying the original price with the discount percentage divided by 100.
Then, it subtracts the discount amount from the original price to get the discounted price. Finally, it returns the
discounted price.

To test the function, we assign example values to the `original_price` and `discount_percentage` variables. You
can change these values to test the function with different original prices and discount percentages.

We then call the `calculate_discount` function with the assigned values for `original_price` and
`discount_percentage`. The function calculates the discounted price and returns it. We store the returned value
in the `discounted_price` variable.

Finally, we print the discounted price to display the result.

You can test the function with different original prices and discount percentages by changing the values of
`original_price` and `discount_percentage` variables before running the program.
Solutions: Implementation of the `calculate_discount` function that takes the original price and a discount
percentage as parameters and returns the discounted price:

def calculate_discount(original_price, discount_percentage):


"""
This function takes the original price and a discount percentage as input
and returns the discounted price.
"""
discount = original_price * (discount_percentage / 100)
discounted_price = original_price - discount
return discounted_price

# Test the function


original_price = float(input("Enter the original price: "))
discount_percentage = float(input("Enter the discount percentage: "))

discounted_price = calculate_discount(original_price, discount_percentage)


print("Discounted price: ", discounted_price)

In this program, the `calculate_discount` function takes the original price and discount percentage as parameters.
It calculates the discount amount by multiplying the original price with the discount percentage divided by 100.
Then, it subtracts the discount amount from the original price to get the discounted price. Finally, it returns the
discounted price.

To test the function, we prompt the user to enter the original price and discount percentage using the `input`
function. We convert the inputs to floating-point numbers using `float` to handle decimal values.

Then, we call the `calculate_discount` function with the entered original price and discount percentage. The
function calculates the discounted price and returns it. We store the returned value in the `discounted_price`
variable.

Finally, we print the discounted price to display the result.

You can test the function with different original prices and discount percentages by running the program multiple
times and entering different values.
13. Implement a function called `find_common_elements` that takes two lists as parameters and
returns a new list containing the common elements between the two lists. Test the function with
different lists.
Solutions: Implementation of the `find_common_elements` function that takes two lists as parameters and
returns a new list containing the common elements between the two lists:

def find_common_elements(list1, list2):


"""
This function takes two lists as input and returns a new list containing the common elements
between the two lists.
"""
common_elements = []
for element in list1:
if element in list2:
common_elements.append(element)
return common_elements

# Test the function


list1 = [1, 2, 3, 4, 5] # Example list 1
list2 = [4, 5, 6, 7, 8] # Example list 2

common_elements = find_common_elements(list1, list2)


print("Common elements:", common_elements)

In this program, the `find_common_elements` function takes two lists, `list1` and `list2`, as parameters. It
initializes an empty list called `common_elements` to store the common elements between the two lists.

The function then iterates over each element in `list1` using a `for` loop. Inside the loop, it checks if the current
element is present in `list2` using the `in` operator. If the element is found in `list2`, it appends it to the
`common_elements` list.

After iterating through all the elements in `list1`, the function returns the `common_elements` list containing the
common elements between the two input lists.
To test the function, we assign example values to the `list1` and `list2` variables. You can change these values
to test the function with different lists.

We then call the `find_common_elements` function with the assigned values for `list1` and `list2`. The function
finds the common elements between the two lists and returns them as a new list. We store the returned list in
the `common_elements` variable.

Finally, we print the `common_elements` list to display the result.

You can test the function with different lists by modifying the values of `list1` and `list2` before running the
program.
Solutions: An updated implementation of the `find_common_elements` function that takes two lists as
parameters using the input method and returns a new list containing the common elements between the two
lists:

def find_common_elements():
"""
This function takes two lists as input using the input method and returns a new list
containing the common elements between the two lists.
"""
list1 = input("Enter the elements of list 1 (space-separated): ").split()
list2 = input("Enter the elements of list 2 (space-separated): ").split()

# Convert input elements to integers (assuming integer elements in the lists)


list1 = [int(element) for element in list1]
list2 = [int(element) for element in list2]

common_elements = []
for element in list1:
if element in list2:
common_elements.append(element)
return common_elements

# Test the function


common_elements = find_common_elements()
print("Common elements:", common_elements)

In this updated program, the `find_common_elements` function prompts the user to enter the elements of `list1`
and `list2` using the `input` function. The user should provide the elements separated by spaces.

After obtaining the input, the function converts the input elements to integers assuming integer elements in the
lists. This is done using a list comprehension.

The function then proceeds to find the common elements between `list1` and `list2` by iterating over `list1` and
checking if each element is present in `list2`. The common elements are appended to the `common_elements`
list.

Finally, the function returns the `common_elements` list containing the common elements between the two input
lists.

To test the function, simply run the program. It will prompt you to enter the elements of `list1` and `list2`. After
entering the elements, the function will find the common elements between the two lists and display them as the
output.

You can test the function with different lists by providing different inputs for `list1` and `list2` when prompted.
14. Create a function called `calculate_factorial` that takes an integer as a parameter and returns the
factorial of that number. Test the function with different numbers.
Solutions: An updated version of the `calculate_factorial` function that takes an integer as a parameter and
returns the factorial of that number without using an input method:

def calculate_factorial(number):
"""
This function takes an integer as input and returns the factorial of that number.
"""
factorial = 1
for i in range(1, number + 1):
factorial *= i
return factorial

# Test the function with different numbers


numbers = [5, 7, 3, 10]
for number in numbers:
factorial = calculate_factorial(number)
print(f"The factorial of {number} is: {factorial}")

In this program, the `calculate_factorial` function is defined with a single parameter `number`. The function
computes the factorial of `number` using a `for` loop and returns the result.

To test the function, a list of numbers (`numbers`) is defined. The program iterates over each number in the list
and calculates its factorial using the `calculate_factorial` function. The result is then printed using formatted string
literals (`f-string`).

You can test the function with different numbers by modifying the `numbers` list or adding more numbers to it.
The program will calculate and print the factorial for each number in the list.
Solutions: Implementation of the `calculate_factorial` function that takes an integer as a parameter and returns
the factorial of that number:

def calculate_factorial(number):
"""
This function takes an integer as input and returns the factorial of that number.
"""
factorial = 1
for i in range(1, number + 1):
factorial *= i
return factorial

# Test the function


number = int(input("Enter a number: "))
factorial = calculate_factorial(number)
print("Factorial:", factorial)

In this program, the `calculate_factorial` function takes an integer `number` as a parameter. It uses a `for` loop
to iterate from 1 to `number` (inclusive) and multiplies each number in the range to the `factorial` variable. The
initial value of `factorial` is set to 1.

After the loop, the function returns the `factorial` value, which represents the factorial of the input number.

To test the function, the program prompts the user to enter a number using the `input` function. The input is then
converted to an integer using the `int` function.

The program then calls the `calculate_factorial` function with the entered number as the argument and assigns
the returned value to the `factorial` variable.

Finally, the program displays the calculated factorial by printing the value of the `factorial` variable.

You can test the function with different numbers by providing different inputs when prompted.
15. Write a function called `is_leap_year` that takes a year as a parameter and returns `True` if it is a
leap year and `False` otherwise. Test the function with different years.
Solutions: The `is_leap_year` function that takes a year as a parameter and returns `True` if it is a leap year
and `False` otherwise:

def is_leap_year(year):
"""
This function takes a year as input and returns True if it is a leap year, False otherwise.
"""
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
else:
return True
else:
return False

# Test the function with different years


years = [2000, 2020, 1900, 2024, 2100]
for year in years:
if is_leap_year(year):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")

In this program, the `is_leap_year` function takes a year as input. It checks if the year is divisible by 4. If it is, the
function further checks if it is divisible by 100. If it is divisible by 100, it checks if it is divisible by 400 as well. If
all the conditions are met, the function returns `True` indicating that it is a leap year. Otherwise, it returns `False`.

To test the function, a list of years (`years`) is defined. The program iterates over each year in the list and checks
if it is a leap year using the `is_leap_year` function. Based on the result, it prints whether the year is a leap year
or not.

You can test the function with different years by modifying the `years` list or adding more years to it. The program
will determine and print whether each year in the list is a leap year or not.
Solutions: The modified version of the `is_leap_year` function that takes a year as a parameter using input
method and returns `True` if it is a leap year and `False` otherwise:

def is_leap_year():
"""
This function takes a year as input and returns True if it is a leap year, False otherwise.
"""
year = int(input("Enter a year: "))
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
else:
return True
else:
return False

# Test the function with different years


years = [2000, 2020, 1900, 2024, 2100]
for year in years:
if is_leap_year(year):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")

In this modified version, the `is_leap_year` function does not take any parameters. Instead, it uses the `input`
function to prompt the user to enter a year. The year is then converted to an integer using `int()` before checking
if it is a leap year.

To test the function, a list of years (`years`) is defined. The program iterates over each year in the list and checks
if it is a leap year using the `is_leap_year` function. Based on the result, it prints whether the year is a leap year
or not.

You can test the function with different years by running the program and entering a year when prompted. The
program will determine and print whether the entered year is a leap year or not.
16. Implement a function called `remove_duplicates` that takes a list as a parameter and returns a
new list with duplicate elements removed. Test the function with different lists.
Solutions: Implementation of the `remove_duplicates` function that takes a list as a parameter and returns a
new list with duplicate elements removed:

def remove_duplicates(input_list):
"""
This function takes a list as input and returns a new list with duplicate elements removed.
"""
return list(set(input_list))

# Test the function with different lists


lists = [[1, 2, 3, 4, 5], [1, 2, 2, 3, 4, 4, 5, 5], ['apple', 'banana', 'cherry', 'apple', 'banana']]
for lst in lists:
result = remove_duplicates(lst)
print(f"Original list: {lst}")
print(f"List with duplicates removed: {result}")
print()

In this implementation, the `remove_duplicates` function takes a list (`input_list`) as a parameter. It uses the `set`
data structure to remove duplicate elements from the input list, as sets only contain unique elements. The
resulting set is then converted back to a list using the `list` function and returned.

To test the function, a list of lists (`lists`) is defined. The program iterates over each list in the `lists` list and calls
the `remove_duplicates` function on each list. The original list and the list with duplicate elements removed are
then printed.

You can test the function with different lists by running the program. The program will print the original list and
the list with duplicate elements removed for each test case.

Solutions: The updated implementation of the `remove_duplicates` function that takes a list as a parameter
using the input method and returns a new list with duplicate elements removed:

def remove_duplicates():
"""
This function takes a list as input using the input method and returns a new list with duplicate elements
removed.
"""
input_list = input("Enter a list of elements (space-separated): ").split()
return list(set(input_list))

# Test the function with different lists


result = remove_duplicates()
print(f"List with duplicates removed: {result}")
In this implementation, the `remove_duplicates` function does not take any parameter. Instead, it uses the input
method (`input`) to prompt the user to enter a list of elements, which are then split into individual elements using
the `split` method.

The list is then converted to a set using the `set` function to remove duplicate elements, and finally converted
back to a list using the `list` function. The resulting list with duplicate elements removed is then returned.

To test the function, you can run the program. The program will prompt you to enter a list of elements, separated
by spaces. After you provide the input, it will print the resulting list with duplicate elements removed.
17. Create a function called `encrypt_message` that takes a string and a key as parameters and
encrypts the message using a simple substitution cipher. Test the function with different messages
and keys.
Solutions: Implementation of the `encrypt_message` function that takes a string and a key as parameters and
encrypts the message using a simple substitution cipher:

def encrypt_message(message, key):


"""
This function takes a string message and a key as parameters and encrypts the message using a simple
substitution cipher.
"""
encrypted_message = ""
for char in message:
if char.isalpha():
if char.isupper():
encrypted_char = chr((ord(char) - 65 + key) % 26 + 65)
else:
encrypted_char = chr((ord(char) - 97 + key) % 26 + 97)
encrypted_message += encrypted_char
else:
encrypted_message += char
return encrypted_message

# Test the function with different messages and keys


message = input("Enter a message to encrypt: ")
key = int(input("Enter the encryption key: "))

encrypted_message = encrypt_message(message, key)


print("Encrypted message:", encrypted_message)

In this implementation, the `encrypt_message` function takes a string `message` and an integer `key` as
parameters. It initializes an empty string `encrypted_message` to store the encrypted message.

The function then iterates over each character in the `message`. If the character is alphabetic, it checks if it is
uppercase or lowercase using the `isupper` method. Based on the case, it applies the appropriate encryption
formula using the ASCII values of the characters. The encrypted character is obtained using the `chr` function.

If the character is not alphabetic, it is added as it is to the `encrypted_message`.

After processing all the characters, the function returns the `encrypted_message`.

To test the function, you can run the program. It will prompt you to enter a message and the encryption key. After
you provide the input, it will encrypt the message using the substitution cipher and print the encrypted message.
Solutions: Implementation of the `encrypt_message` function that takes a string and a key as parameters
without using the input method and encrypts the message using a simple substitution cipher:

def encrypt_message(message, key):


"""
This function takes a string message and a key as parameters and encrypts the message using a simple
substitution cipher.
"""
encrypted_message = ""
for char in message:
if char.isalpha():
if char.isupper():
encrypted_char = chr((ord(char) - 65 + key) % 26 + 65)
else:
encrypted_char = chr((ord(char) - 97 + key) % 26 + 97)
encrypted_message += encrypted_char
else:
encrypted_message += char
return encrypted_message

# Test the function with different messages and keys


message = "Hello, World!"
key = 3

encrypted_message = encrypt_message(message, key)


print("Encrypted message:", encrypted_message)

In this implementation, the `encrypt_message` function takes a string `message` and an integer `key` as
parameters. It initializes an empty string `encrypted_message` to store the encrypted message.

The function then iterates over each character in the `message`. If the character is alphabetic, it checks if it is
uppercase or lowercase using the `isupper` method. Based on the case, it applies the appropriate encryption
formula using the ASCII values of the characters. The encrypted character is obtained using the `chr` function.

If the character is not alphabetic, it is added as it is to the `encrypted_message`.

After processing all the characters, the function returns the `encrypted_message`.

To test the function, you can assign a message and key directly in the code. After running the program, it will
encrypt the message using the substitution cipher and print the encrypted message.
18. Write a function called `calculate_average_grade` that takes a dictionary of student grades as a
parameter and returns the average grade across all students. Test the function with different
dictionaries of grades.
Solutions: Implementation of the `calculate_average_grade` function that takes a dictionary of student grades
as a parameter and returns the average grade across all students:

def calculate_average_grade(grades):
"""
This function takes a dictionary of student grades as a parameter and returns the average grade across all
students.
"""
total_grades = 0
num_students = len(grades)

for student, grade in grades.items():


total_grades += grade

average_grade = total_grades / num_students


return average_grade

# Test the function with different dictionaries of grades


grades1 = {"Alice": 85, "Bob": 92, "Charlie": 78, "David": 89}
grades2 = {"Alice": 91, "Bob": 87, "Charlie": 95, "David": 82, "Eve": 90}

average_grade1 = calculate_average_grade(grades1)
print("Average grade 1:", average_grade1)

average_grade2 = calculate_average_grade(grades2)
print("Average grade 2:", average_grade2)

In this implementation, the `calculate_average_grade` function takes a dictionary `grades` as a parameter. It


initializes variables `total_grades` to keep track of the sum of grades and `num_students` to store the number of
students.

The function then iterates over each key-value pair in the `grades` dictionary using the `items()` method. For
each student, it adds their grade to the `total_grades` variable.

After processing all the grades, it calculates the `average_grade` by dividing the `total_grades` by the
`num_students`.

Finally, the function returns the `average_grade`.

To test the function, you can create different dictionaries of grades. After running the program, it will calculate
and print the average grade for each dictionary.
Solutions: Implementation of the `calculate_average_grade` function that takes a dictionary of student grades
as a parameter using input method and returns the average grade across all students:

def calculate_average_grade(grades):
"""
This function takes a dictionary of student grades as a parameter and returns the average grade across all
students.
"""
total_grades = sum(grades.values())
num_students = len(grades)

average_grade = total_grades / num_students


return average_grade

# Test the function with different dictionaries of grades


grades1 = {}
n = int(input("Enter the number of students: "))
for i in range(n):
student = input("Enter the name of student: ")
grade = float(input("Enter the grade of student: "))
grades1[student] = grade

average_grade1 = calculate_average_grade(grades1)
print("Average grade 1:", average_grade1)

grades2 = {}
m = int(input("Enter the number of students: "))
for i in range(m):
student = input("Enter the name of student: ")
grade = float(input("Enter the grade of student: "))
grades2[student] = grade

average_grade2 = calculate_average_grade(grades2)
print("Average grade 2:", average_grade2)

In this implementation, the `calculate_average_grade` function takes a dictionary `grades` as a parameter. It


uses the `sum()` function to calculate the sum of all the grades in the dictionary and the `len()` function to get
the number of students.

The function then calculates the `average_grade` by dividing the `total_grades` by the `num_students`.

To test the function, you can input the number of students and their grades for each dictionary. After running the
program, it will calculate and print the average grade for each dictionary.
19. Implement a function called `check_anagram` that takes two strings as parameters and returns
`True` if they are anagrams (contain the same characters) and `False` otherwise. Test the function
with different pairs of strings.
Solutions: Implementation of the `check_anagram` function that takes two strings as parameters and returns
`True` if they are anagrams (contain the same characters) and `False` otherwise:

def check_anagram(string1, string2):


"""
This function takes two strings as parameters and returns True if they are anagrams and False otherwise.
"""
# Convert the strings to lowercase and remove whitespace
string1 = string1.lower().replace(" ", "")
string2 = string2.lower().replace(" ", "")

# Check if the lengths of the strings are equal


if len(string1) != len(string2):
return False

# Create dictionaries to store the counts of each character


count1 = {}
count2 = {}

# Count the occurrences of each character in string1


for char in string1:
count1[char] = count1.get(char, 0) + 1

# Count the occurrences of each character in string2


for char in string2:
count2[char] = count2.get(char, 0) + 1

# Compare the dictionaries


if count1 == count2:
return True
else:
return False

# Test the function with different pairs of strings


string1 = input("Enter the first string: ")
string2 = input("Enter the second string: ")

if check_anagram(string1, string2):
print("The strings are anagrams.")
else:
print("The strings are not anagrams.")

In this implementation, the `check_anagram` function takes two strings `string1` and `string2` as parameters. It
first converts the strings to lowercase and removes any whitespace using the `lower()` and `replace()` methods.

The function then checks if the lengths of the strings are equal. If they are not, it returns `False` since they cannot
be anagrams.

Next, it creates two dictionaries `count1` and `count2` to store the counts of each character in `string1` and
`string2`, respectively. It uses a loop to iterate over each character in the strings and updates the counts in the
dictionaries using the `get()` method.

Finally, the function compares the two dictionaries. If they are equal, it returns `True`, indicating that the strings
are anagrams. Otherwise, it returns `False`.

To test the function, you can input two strings and it will determine if they are anagrams or not. After running the
program, it will print the appropriate message indicating whether the strings are anagrams or not.

Solutions: Implementation of the `check_anagram` function that takes two strings as parameters without using
the input method and returns `True` if they are anagrams (contain the same characters) and `False` otherwise:

def check_anagram(string1, string2):


"""
This function takes two strings as parameters and returns True if they are anagrams and False otherwise.
"""
# Convert the strings to lowercase and remove whitespace
string1 = string1.lower().replace(" ", "")
string2 = string2.lower().replace(" ", "")

# Check if the lengths of the strings are equal


if len(string1) != len(string2):
return False

# Create dictionaries to store the counts of each character


count1 = {}
count2 = {}

# Count the occurrences of each character in string1


for char in string1:
count1[char] = count1.get(char, 0) + 1

# Count the occurrences of each character in string2


for char in string2:
count2[char] = count2.get(char, 0) + 1
# Compare the dictionaries
if count1 == count2:
return True
else:
return False

# Test the function with different pairs of strings


string1 = "listen"
string2 = "silent"

if check_anagram(string1, string2):
print("The strings are anagrams.")
else:
print("The strings are not anagrams.")

In this implementation, the `check_anagram` function is defined with the same logic as before. However, instead
of using the input method to get the strings from the user, the strings are provided directly in the code.

You can modify the values of `string1` and `string2` to test the function with different pairs of strings. After running
the program, it will print the appropriate message indicating whether the strings are anagrams or not.
20. Create a function called `convert_to_binary` that takes an integer as a parameter and returns its
binary representation as a string. Test the function with different numbers.
Solutions: Implementation of the `convert_to_binary` function that takes an integer as a parameter and returns
its binary representation as a string:

def convert_to_binary(number):
"""
This function takes an integer as a parameter and returns its binary representation as a string.
"""
binary = bin(number)[2:] # Convert the number to binary and remove the "0b" prefix
return binary

# Test the function with different numbers


number1 = 10
number2 = 27
number3 = 42

binary1 = convert_to_binary(number1)
binary2 = convert_to_binary(number2)
binary3 = convert_to_binary(number3)

print(f"The binary representation of {number1} is: {binary1}")


print(f"The binary representation of {number2} is: {binary2}")
print(f"The binary representation of {number3} is: {binary3}")

In this implementation, the `convert_to_binary` function uses the built-in `bin()` function to convert the given
number to its binary representation. The binary representation is returned as a string.

You can modify the values of `number1`, `number2`, and `number3` to test the function with different numbers.
After running the program, it will print the binary representation of each number.

Solutions: Implementation of the `convert_to_binary` function that takes an integer as a parameter using the
`input()` method and returns its binary representation as a string:

def convert_to_binary():
"""
This function takes an integer as input and returns its binary representation as a string.
"""
number = int(input("Enter an integer: "))
binary = bin(number)[2:] # Convert the number to binary and remove the "0b" prefix
return binary

# Test the function with different numbers


binary1 = convert_to_binary()
binary2 = convert_to_binary()
binary3 = convert_to_binary()

print(f"The binary representation is: {binary1}")


print(f"The binary representation is: {binary2}")
print(f"The binary representation is: {binary3}")

In this implementation, the `convert_to_binary` function prompts the user to enter an integer using the `input()`
function. The input is converted to an integer using `int()` and then converted to its binary representation using
the built-in `bin()` function. The binary representation is returned as a string.

You can run the program and enter different integers to test the function. It will print the binary representation of
each entered number.

You might also like