WEBVTT

00:00.000 --> 00:12.560
In this video, we will learn how to use functions in Python, how to build an interactive

00:12.560 --> 00:16.240
program.

00:16.240 --> 00:21.320
We are creating a program that functions as a mini toolbox with two tools, a calculator

00:21.320 --> 00:27.200
for basic arithmetic operations, and a temperature converter between Celsius and Fahrenheit.

00:27.200 --> 00:31.920
We get to choose which tool to use, and the program will guide you step-by-step.

00:31.920 --> 00:38.240
In Python, functions help us organize code by breaking tasks into smaller, reusable

00:38.240 --> 00:40.720
parts.

00:40.720 --> 00:46.160
Defining functions for operations Here, we use functions to perform basic

00:46.160 --> 00:53.080
arithmetic operations like addition, subtraction, multiplication, and division.

00:53.080 --> 01:03.280
Def add, a b, return, a plus b, def subtract a b, return a minus b, def multiply a b, return

01:03.280 --> 01:09.600
a asterisk b, def divide a b, return error.

01:09.600 --> 01:17.640
If b is equal to 0, else a by b, each function takes two numbers as input and returns the result.

01:17.640 --> 01:21.920
Def invalid operation a, b, return invalid.

01:21.920 --> 01:29.000
Invalid operation a, b is used to return invalid, if the user enters an incorrect operation.

01:29.000 --> 01:39.600
Function-based calculator, def calculator, print, choose, add, subtract, multiply, divide.

01:39.600 --> 01:44.360
Operation is equal to input, enter operation dot lower.

01:44.360 --> 01:54.640
Sum 1, num 2 is equal to float, input, first number, float, input, second number.

01:54.640 --> 02:01.600
The calculator function asks the user for an operation and the input of two numbers.

02:01.600 --> 02:10.320
Operations is equal to add add, subtract, subtract, multiply, multiply, divide, divide.

02:10.320 --> 02:18.240
Expanded function is equal to operations dot get, operation, invalid operation, print, result.

02:18.240 --> 02:23.480
Selected function num 1 num 2, it uses addictionary operations to call the correct function based

02:23.480 --> 02:27.040
on the user's choice.

02:27.040 --> 02:29.720
Temperature converter function.

02:29.720 --> 02:35.480
Temperature converter function contains two nested functions, c to f, c, convert Celsius

02:35.480 --> 02:42.480
to Fahrenheit, f to c, f, converts Fahrenheit to Celsius, def temperature converter, def

02:42.480 --> 02:52.720
c to f, c, return, c, asterisk 9 by 5, plus 32, def to c, f, return, f, minus 32, asterisk

02:52.720 --> 02:59.880
5 by 9, print, convert temperature, c to f or f to c, choice is equal to input, type

02:59.880 --> 03:11.120
c or f, dot upper, temp is equal to float, input, enter temperature, print, result, c to

03:11.120 --> 03:19.120
f, temp, if choice is equal to c, else f to c, temp, degree, it prompts the user to choose

03:19.120 --> 03:24.040
a conversion type, asks for input, and then calls the appropriate function to perform

03:24.040 --> 03:32.960
the conversion, main function to control program flow, def main, print, choose a program,

03:32.960 --> 03:42.560
1, calculator, 2, temperature converter, choice is equal to input, enter 1 or 2, if choice

03:42.560 --> 03:51.420
is equal to 1, calculator, else if, choice is equal to 2, temperature converter, else,

03:51.420 --> 03:59.220
print, invalid choice, try again, return main, main function starts by asking the user

03:59.220 --> 04:04.780
to choose between the calculator and temperature converter, if user enters an invalid choice,

04:04.780 --> 04:10.500
it prompts them to try again, after running 1 function, the user gets an extended menu

04:10.500 --> 04:17.380
with 3 options, use the calculator again, use the temperature converter again, or exit

04:17.380 --> 04:23.620
the program, here we are using a wild loop to keep showing this menu until the user chooses

04:23.620 --> 04:29.100
to exit, allowing them to perform multiple calculations or conversions without restarting

04:29.100 --> 04:40.620
the program, while true, print, choose 1, calculator, 2, temperature converter, 3, exit,

04:40.620 --> 04:49.980
choice is equal to input, enter 1, 2, or 3, if choice is equal to 1, calculator, else

04:49.980 --> 04:58.860
if, choice is equal to 2, temperature converter, else if, choice is equal to 3, print, exiting

04:58.860 --> 05:08.900
program, break, else, print, invalid choice, try again, main, save the program with a name

05:08.900 --> 05:14.380
like function-based calculator.py, click the run button and test the program by choosing

05:14.380 --> 05:21.020
different options, first we choose calculator and select add, we enter numbers 20 and 30,

05:21.020 --> 05:28.460
and the program displays, result 50, next we choose calculator again and select multiply,

05:28.460 --> 05:36.700
we enter 6 and 5, and the program displays, result, 30, now we select temperature converter,

05:36.700 --> 05:44.260
choose seat, and enter 20 degrees Celsius, the program displays, result, 68.0 degrees

05:44.260 --> 05:51.220
Fahrenheit, finally we choose exit and the program stops, functions make programs efficient

05:51.220 --> 05:56.020
and organized, this program used multiple functions for clarity and ease of use.

