-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathfunctions_examples
More file actions
85 lines (66 loc) · 2.46 KB
/
functions_examples
File metadata and controls
85 lines (66 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
############################################### ########### Mr Barry Area of triangle
def calculate_rectangle_area(length, width):
"""
Calculate the area of a rectangle.
Parameters:
- length (float): Length of the rectangle.
- width (float): Width of the rectangle.
Returns:
- float: Area of the rectangle.
"""
area = length * width
return area
# Get user input for length and width
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
# Call the function and display the result
area_result = calculate_rectangle_area(length, width)
print(f"The area of the rectangle is: {area_result}")
################################################# temperature
def celsius_to_fahrenheit(celsius):
"""
Convert temperature from Celsius to Fahrenheit.
Parameters:
- celsius (float): Temperature in Celsius.
Returns:
- float: Temperature in Fahrenheit.
"""
fahrenheit = (celsius * 9/5) + 32
return fahrenheit
# Get user input for temperature in Celsius
celsius_temp = float(input("Enter temperature in Celsius: "))
# Call the function and display the result
fahrenheit_result = celsius_to_fahrenheit(celsius_temp)
print(f"{celsius_temp} degrees Celsius is {fahrenheit_result} degrees Fahrenheit.")
############################################### Calculator
def simple_calculator(num1, num2, operation):
"""
Perform a simple arithmetic operation.
Parameters:
- num1 (float): First operand.
- num2 (float): Second operand.
- operation (str): Arithmetic operation ('+', '-', '*', '/').
Returns:
- float: Result of the operation.
"""
if operation == '+':
result = num1 + num2
elif operation == '-':
result = num1 - num2
elif operation == '*':
result = num1 * num2
elif operation == '/':
result = num1 / num2
else:
result = None
print("Invalid operation!")
return result
###################################################### Calculator
# Get user input for numbers and operation
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
operation_input = input("Enter the operation (+, -, *, /): ")
# Call the function and display the result
calculator_result = simple_calculator(num1, num2, operation_input)
if calculator_result is not None:
print(f"The result of {num1} {operation_input} {num2} is: {calculator_result}")