Python Exercise 3

Mannan Ul Haq
0

Objective:

After performing this exercise, you will be able to solve programming problems using:
  1. 'if' statements
  2. 'elif' statement
  3. Nested 'if' structure
  4. 'while' loop
  5. 'for' loop

Activity 1:

Write a program to input electricity unit charges and calculate total electricity bill according to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill

Solution:

unit = float(input("Enter Total Units Consumed: "))

if unit <= 50:
    amt = unit * 0.50
elif unit <= 150:
    amt = 25 + ((unit - 50) * 0.75)
elif unit <= 250:
    amt = 100 + ((unit - 150) * 1.20)
else:
    amt = 220 + ((unit - 250) * 1.50)

sur_charge = amt * 0.20
total_amt = amt + sur_charge

print("Electricity Bill =", total_amt)



Activity 2:

Write a program that calculates and displays a person’s body mass index (BMI). The BMI is often used to determine whether a person with a sedentary lifestyle is overweight or underweight for his or her height. A person’s BMI is calculated with the following formula:

BMI = weight x 703 / height?

Where weight is measured in pounds and height is measured in inches. The program should display a message indicating whether the person has optimal weight, is underweight, or is overweight. A person’s weight is considered to be optimal if his or her BMI is between 18.5 and 25. If the BMI is less than 18.5, the person is considered to be underweight. If the BMI value is greater than 25, the person is considered to be overweight.

Solution:

weight = float(input("Enter Weight in pounds: "))
height = float(input("Enter Height in inches: "))

bmi = (weight * 703) / (height ** 2)
print("Your Body Mass Index =", bmi)

if bmi > 25.0:
    print("The Person is Overweight!")
elif 18.5 <= bmi <= 25.0:
    print("The Person's Weight is Optimal!")
else:
    print("The Person is Underweight!")



Activity 3:

Take input of age of 3 people by the user and determine the oldest and youngest among them.

Solution:

a = int(input("Type the age of First Person: "))
b = int(input("Type the age of Second Person: "))
c = int(input("Type the age of Third Person: "))

if a > b:
    if c < b:
        print("The First Person is Oldest")
        print("The Third Person is Youngest")
    else:
        print("The First Person is Oldest")
        print("The Second Person is Youngest")
elif b > a:
    if c > a:
        print("The Second Person is Oldest")
        print("The Third Person is Youngest")
    else:
        print("The Second Person is Oldest")
        print("The First Person is Youngest")
else:  # Assuming the condition is c > a and c > b
    if a > b:
        print("The Third Person is Oldest")
        print("The First Person is Youngest")
    else:
        print("The Third Person is Oldest")
        print("The Second Person is Youngest")



Activity 4:

Write a program to find the eligibility of admission for a professional course based on the following criteria: Marks in Maths >=65 and Marks in Phy >=55 and Marks in Chem>=50 and Total in all three subject >=190 or Total in Maths and Physics >=140


Solution:

p = int(input("Input the marks obtained in Physics: "))
c = int(input("Input the marks obtained in Chemistry: "))
m = int(input("Input the marks obtained in Mathematics: "))

if (m >= 65 and p >= 55 and c >= 50 and (m + p + c) >= 190) or (m + p) >= 140:
    print("The Candidate is eligible for admission.")
else:
    print("The Candidate is not eligible.")


Activity 5:

Print the Fibonacci sequence (0,1,1,2,3,5,8,13,21, 34,……..) till the N member. Take N from user input. Fibonacci sequence is given by formula: sum the last two numbers to get the current.

Solution:

n = int(input("Enter the number up to which you want to print Fibonacci Sequence: "))
a, b = 0, 1
while a <= n:
    print(a, end=", ")
    a, b = b, a + b



Activity 6: 

Write a program that prompts the user to enter the values. Get the input again and again until user enter -1 so the condition to break the loop is -1. Your task is to determine the largest and smallest value entered by the user.

Solution:

print("Enter Numbers:")

n1 = int(input())
large = n1
small = n1

while True:
    n2 = int(input())
    
    if n2 == -1:
        break
    if n2 > large:
        large = n2
    elif n2 < small and n2 >= 0:
        small = n2

print("The Largest Number is", large)
print("The Smallest Numbers is", small)



Activity 7: 

Write a Program to Calculate the Factorial of a Positive Integer. The program repeats the process until the user enters 0.

Solution:

i = 1

while i == 1:
    n = int(input("Enter a Positive integer: "))

    if n < 0:
        print("Error! Factorial of a negative number doesn't exist.")
        break
    else:
        fact = 1
        for a in range(1, n + 1):
            fact *= a
        print("Factorial of", n, "is", fact)

    i = int(input("If you want to Calculate another factorial press 1 otherwise press 0: "))



Activity 8: 

Write a program that gets starting and ending point from the user and displays all odd numbers in the given range using do-while loop.

Solution:

start = int(input("Enter Starting Number: "))
end = int(input("Enter Ending Number: "))

print("All Numbers between", start, "and", end, "are:")
num = start

while num <= end:
    if num % 2 != 0:
        print(num)
    num += 1



Activity 9: 

Write a program that inputs table number and length of table and then displays the table using for loop.

Solution: 

num = int(input("Enter Table Number: "))
length = int(input("Enter Length of Table: "))

for i in range(1, length + 1):
    print(num, "*", i, "=", num * i)



Activity 10:

 Write a program to print the following output using loop:


- - - - - * - - - - - 

- - - - * - * - - - -

 - - - * - - - * - - -

 - - * - - - - - * - - 

- * - - - - - - - * - 

* - - - - - - - - - *


Solution:

for i in range(1, 7):
    line = ''
    # Add leading spaces to the line
    for j in range(1, 7 - i):
        line += " "
    # Add pattern to the line
    for k in range(1, i * 2):
        if k == 1 or k == i * 2 - 1:
            line += "*"
        elif i == 6:
            line += "*"
        else:
            line += " "
    print(line)



Activity 11:

Write a program that displays the following using do-while loop.

4 4 4 4

3 3 3

2 2

1


Solution:

row = 4

while row > 0:
    line = ""
    column = 1
    while column <= row:
        line += str(row)
        column += 1
    print(line)
    row -= 1



Activity 12: 

Write a program to print the following pyramid pattern using loop:

*

 * * * 

* * * * * 

* * * * * * * 

* * * * * * * * *


Solution:

rows = int(input("Enter number of rows: "))

for i in range(1, rows + 1):
    line = ""
    
    # Add spaces to the line
    for space in range(1, rows - i + 1):
        line += "  "
    
    # Add asterisks to the line
    j = 0
    while j != 2 * i - 1:
        line += "* "
        j += 1
    
    print(line)


Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !