0% found this document useful (0 votes)
14 views6 pages

LAB2

The document provides instructions for converting Fahrenheit to Celsius, calculating a student's test percentage, and computing the volume of a sphere and the area of a triangle using Python functions. It includes formulas for each calculation and error handling for invalid inputs. Additionally, it outlines how to take user input and display the results.

Uploaded by

Mohamed Ayman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

LAB2

The document provides instructions for converting Fahrenheit to Celsius, calculating a student's test percentage, and computing the volume of a sphere and the area of a triangle using Python functions. It includes formulas for each calculation and error handling for invalid inputs. Additionally, it outlines how to take user input and display the results.

Uploaded by

Mohamed Ayman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1) Ahmed needs to change a Fahrenheit temperature to Celsius.

Draw a flowchart
to convert from Fahrenheit to Celsius. C = 5/9 (F – 32)
An instructor calculates the grade percentage based on the highest score on a test. Given the
highest score and one student’s score, draw a flowchart to calculate and print that
student’s test percentage.
Percentage = Score/Highest Score * 100
3- Write a solution to compute Sphere Volume Sphere Volume = 4/3 * Pi * (radius)3

import math

def compute_sphere_volume(radius):

"""

Computes the volume of a sphere given its radius.

Formula: Volume = (4/3) * π * (radius^3)

"""

if radius < 0:

return "Radius cannot be negative. Please enter a valid radius."

volume = (4 / 3) * math.pi * (radius ** 3)

return volume

# Main code to take input from the user

if __name__ == "__main__":

try:

# Get the radius from the user

radius = float(input("Enter the radius of the sphere: "))

# Calculate and print the volume

volume = compute_sphere_volume(radius)

print(f"The volume of the sphere is: {volume:.2f}")

except ValueError:
print("Please enter a valid number for the radius.")
4- Write a solution to compute Triangle Area?

Area=.5×Base×Height

def compute_triangle_area(base, height):

"""

Computes the area of a triangle using the formula:

Area = 0.5 * base * height

"""

if base < 0 or height < 0:

return "Base and height must be non-negative."

area = 0.5 * base * height

return area

# Main code to take input from the user

if __name__ == "__main__":

try:

# Get the base and height from the user

base = float(input("Enter the base of the triangle: "))

height = float(input("Enter the height of the triangle: "))

# Calculate and print the area

area = compute_triangle_area(base, height)

print(f"The area of the triangle is: {area:.2f}")

except ValueError:
print("Please enter valid numbers for base and height.")

You might also like