ICT1011 - Lab - Manual - 3 Answer

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

ABU DHABI POLYTECHNIC

Academic Support Department

Laboratory Manual No. 3

Basic Concept and Syntax of Python Programming


(CONTROL FLOW STATEMENTS – Decision Statements)
(if, if…else)

A.C. 2023-2024
ICT-1011: Introduction to Programming and Problem Solving

CONTROL STATEMENT – IF STATEMENT

1. IF SINGLE SELECTION STATEMENT


Syntax:
if Boolean_ Expression:
statement (s) //if the condition is true

Example:
if x>5:
print(“Hello Human”)

2. IF … ELSE DOUBLE SELECTION STATEMENT


Syntax:
if Boolean_ Expression:
statement (s) //if the condition is true
else:
statement (s) //if the condition is false

Example:
if x>5:
print(“Hello Human”)
else:
print(“How are you”)

PRACTICE:

1. Write a program that will read two numbers. Determine and output the larger number.

Answer No.1 (Using Single Selection if statement)

num1=input("Enter first number: ")

num2=input("Enter second number: ")

if num1>num2:
print("The larger number is ", num1)

if num2>num1:
print("The larger number is ", num2)

Lab Manual No. 3 | Page 2


ICT-1011: Introduction to Programming and Problem Solving

Answer No.2 (Using if…else Statement)

num1=input("Enter first number: ")

num2=input("Enter second number: ")

if num1>num2:
large=num1

else:
large=num2

print("The larger number is ", large)

Lab Manual No. 3 | Page 3


ICT-1011: Introduction to Programming and Problem Solving

Name: rashed abdulla ID: ______________


Reserved Section
Lab: _____________________________ Grade:

Date: __________________________ CRN: ______________

LABORATORY EXERCISES:

Task 1
Write a Python program using if statements that will display a menu of a 5-function calculator
as follows:

+ to add two numbers


- to subtract two numbers
* to multiply two numbers
/ to divide two numbers
** to find the exponent of two numbers

Prompt the user to enter two integer numbers and choose a function from the menu to
perform. Calculate and display the result.

Task 1 Answer (Screenshot + program code)

```python
```

```python
while True:
print("Menu:")
print("+ to add two numbers")
print("- to subtract two numbers")
print("* to multiply two numbers")
print("/ to divide two numbers")
print("** to find the exponent of two numbers")
print("q to quit")

user_choice = input("Enter your choice: ")

if user_choice == 'q':
break

if user_choice in ('+', '-', '*', '/', '**'):


num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

if user_choice == '+':
result = num1 + num2
print("Result:", result)
elif user_choice == '-':
result = num1 - num2
print("Result:", result)

Lab Manual No. 3 | Page 4


ICT-1011: Introduction to Programming and Problem Solving

elif user_choice == '*':


result = num1 * num2
print("Result:", result)
elif user_choice == '/':
if num2 == 0:
print("Error: Division by zero")
else:
result = num1 / num2
print("Result:", result)
elif user_choice == '**':
result = num1 ** num2
print("Result:", result)
else:
print("Invalid choice. Please try again.")
```
Menu:
+ to add two numbers
- to subtract two numbers
* to multiply two numbers
/ to divide two numbers
** to find the exponent of two numbers
q to quit
Enter your choice: **
Enter the first number: 5
Enter the second number: 3
Result: 125
Menu:
+ to add two numbers
- to subtract two numbers
* to multiply two numbers
/ to divide two numbers
** to find the exponent of two numbers
q to quit

```python
```
```python
```

Lab Manual No. 3 | Page 5


ICT-1011: Introduction to Programming and Problem Solving

Lab Manual No. 3 | Page 6


ICT-1011: Introduction to Programming and Problem Solving

Task 2
A food shop will give discount of 15% if the cost of purchased quantity is equal to or more than
1500 AED. No discount will be given for purchases less than 1500 AED.
Write a Python program using if-else statement that will ask the user for the quantity and the
unit price of the food item. Calculate and display the original cost, the discounted amount, and
the cost after discount (if no discount, then the discount field should show zero).

Lab Manual No. 3 | Page 7


ICT-1011: Introduction to Programming and Problem Solving

Task 2 Answer (Screenshot + program code)

quantity = int(input("Enter the quantity of the food item: "))


unit_price_food = float(input("Enter the unit price of the food item (in AED): "))

original_cost = quantity * unit_price_food

if original_cost >= 1500:


discount = 0.15 * original_cost
cost_after_discount = original_cost - discount
else:
discount = 0
cost_after_discount = original_cost

print("Original Cost: {:.2f} AED".format(original_cost))

print("Discount: {:.2f} AED".format(discount))

print("Cost after Discount: {:.2f} AED".format(cost_after_discount))

Lab Manual No. 3 | Page 8

You might also like