0% found this document useful (0 votes)
58 views3 pages

Temperature Conversion Functions

CET313 - Introduction to AI

Uploaded by

9teenMine
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)
58 views3 pages

Temperature Conversion Functions

CET313 - Introduction to AI

Uploaded by

9teenMine
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

Tutorial_Exercises

June 16, 2024

[1]: #Exercise 1
def fahrenheit_to_centigrade(fahrenheit):
"""Converts temperature from Fahrenheit to Centigrade

Args:
fahrenheit: Temperature in Fahrenheit

Returns:
Temperature in Centigrade
"""
centigrade = (fahrenheit - 32) / 1.8
return centigrade

def centigrade_to_fahrenheit(centigrade):
"""Converts temperature from Centigrade to Fahrenheit

Args:
centigrade: Temperature in Centigrade

Returns:
Temperature in Fahrenheit
"""
fahrenheit = (centigrade * 1.8) + 32
return fahrenheit

# Sample temperature values


fahrenheit_val = 100
centigrade_val = 35

# Convert Fahrenheit to Centigrade


converted_celsius = fahrenheit_to_centigrade(fahrenheit_val)
print(f"{fahrenheit_val} Fahrenheit is equal to {converted_celsius:.2f}␣
↪Centigrade")

# Convert Centigrade to Fahrenheit


converted_fahrenheit = centigrade_to_fahrenheit(centigrade_val)

1
print(f"{centigrade_val} Centigrade is equal to {converted_fahrenheit:.2f}␣
↪Fahrenheit")

100 Fahrenheit is equal to 37.78 Centigrade


35 Centigrade is equal to 95.00 Fahrenheit

[7]: #Exercise 2
def calculate_average(numbers):
"""Calculates the average of a list of numbers

Args:
numbers: A list of numbers

Returns:
The average of the numbers in the list, or None if the list is empty
"""
if not numbers:
return None

total = 0
for num in numbers:
total += num
return total / len(numbers)

# Get numbers from user


numbers = []
num_inputs = int(input("Enter the number of values to average: "))

# Loop to get user input for the specified number of times


for _ in range(num_inputs):
number = input("Enter a number: ")
try:
[Link](float(number))
except ValueError:
print("Invalid input. Please enter a number.")

# Calculate and display average


average = calculate_average(numbers)

if average is not None:


print(f"The average of the numbers is: {average:.2f}")
else:
print("No valid numbers were entered.")

Enter the number of values to average: 5


Enter a number: 2
Enter a number: 4

2
Enter a number: 9
Enter a number: 10
Enter a number: 7
The average of the numbers is: 6.40

You might also like