0% found this document useful (0 votes)
3 views

Assignment 3 - Functions (1) - 1

Assignment 3 -

Uploaded by

asamhsmyr707
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Assignment 3 - Functions (1) - 1

Assignment 3 -

Uploaded by

asamhsmyr707
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

‫العربية‬ ‫المملكة‬

The kingdom of Saudi Arabia


‫ السعودية‬Education
Ministry of
King Abdulaziz
‫التعليم‬ ‫ وزارة‬University
‫عبدالعزيز‬ The Applied
‫الملك‬ College
‫جامعة‬
Computer and Information Technology
‫الكلية التطبيقية‬
‫قسم الحاسب اآللي وتقنية‬

ACNT 110: Programming Fundamentals


Assignment 3
Assignment Learning Outcomes:

By the end of this assignment, you will be able to:


 Create functions in Python
 Use the conditional execution capabilities

Scenario:
Write a Python program which have four functions: multiplication, addition, subtraction and division.
Do the following:

Instructions Points
1 Ask the user to enter two values and convert the values into float 2
2 Ask the user to enter option 1 for multiplication, 2 for addition, 3 for 1
subtraction and 4 for division
3 Use if statement to check which option the user has selected and call the 2
appropriate function according to the user’s input.
4 Function should calculate the result and return one value 2
5 Display the result 1
Total 8

Expected output:
If the user enters values 3 and 2:
Enter the first number: 3
Enter the second number: 2
Enter 1 for multiplication, 2 for addition, 3 for subtraction and 4 for division: 4
The result is 1.5

Requirements:
1. Download the assignment document.
2. Copy and paste the Python code into the assignment document.
3. Provide a screenshot for the console screen in the assignment document.
4. Submit the assignment document.

Page 1 of 3
‫العربية‬ ‫المملكة‬
The kingdom of Saudi Arabia
‫ السعودية‬Education
Ministry of
King Abdulaziz
‫التعليم‬ ‫ وزارة‬University
‫عبدالعزيز‬ The Applied
‫الملك‬ College
‫جامعة‬
Computer and Information Technology
‫الكلية التطبيقية‬
‫قسم الحاسب اآللي وتقنية‬

Python Code :

# Function for multiplication


def multiply(c, d):
return c * d

# Function for Summtion


def add(a, b):
return a + b

# Function for subtraction


def subtract(e, f):
return e - f

# Function for division


def divide(g, h):
if h == 0:
return "Error! Division by zero."
else:
return g / h

# Main function to perform the operations


def calculator():
# Taking user input for numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

# Prompting user for operation choice


print("Enter 1 for multiplication, 2 for addition, 3 for subtraction and 4 for division:")
choice = int(input())

# Performing the selected operation


if choice == 1:
print(f"The result is {multiply(num1, num2)}")
elif choice == 2:
print(f"The result is {add(num1, num2)}")
elif choice == 3:
print(f"The result is {subtract(num1, num2)}")
elif choice == 4:
print(f"The result is {divide(num1, num2)}")
else:
print("Invalid input! Please select a valid operation.")

# Calling the calculator function


if __name__ == "__main__":
calculator()

Page 2 of 3
‫العربية‬ ‫المملكة‬
The kingdom of Saudi Arabia
‫ السعودية‬Education
Ministry of
King Abdulaziz
‫التعليم‬ ‫ وزارة‬University
‫عبدالعزيز‬ The Applied
‫الملك‬ College
‫جامعة‬
Computer and Information Technology
‫الكلية التطبيقية‬
‫قسم الحاسب اآللي وتقنية‬

screenshot for the console screen:

Page 3 of 3

You might also like