What is content writing?

The purpose of content writing is to inform, entertain, educate, and persuade the readers to take action. Freelance content writing is not limited to only blog posts or article writing, but it offers…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Functions and Exception Handling in Python

A. Functions

We often use commands in programming, there are functions that are already available, without having to create them first and can be used directly such as int (), str (), print (), input () etc. We can see that the function has a characteristic that is the sign “( )”.

A function is a collection of lines or lines of code that are grouped into a single unit so that they can be called or used many times.

We can also create functions.

Example :

• def above serves as a sign that the program code is a function. Blocks of program code in python are defined by indentation.

• the name of the function is the name of the function of the program that we will create and call

• For variables/parameters it is not mandatory, there may or may not be depending on what program we are going to make

after making a program like the example above what should we do? Is it done? then how to call the function?

The example above is just an explanation, there is still a continuation, namely calling the function that we have created.

The only way to call a function is to just write the name of the function along with the variables or parameters that we have created. Then the output is the result of the command in the function.

output :

if we mention the function n times, then the resulting output is also n times. Example function with parameters as follows

Input :

output :

An example of a function without parameters is as follows

output :

B. Exception Handling

Every time we want to run a program, we must often encounter errors, usually due to syntax errors or grammatical errors, but other than that. There are still other errors in the program. When an error occurs, the program will immediately stop and display some information about what error occurred. We are also often confused by looking for errors that make us dizzy because we can stay up all night just because of one error. Well but there a way so that the program we are working on does not stop when an error occurs. This method is also called exception handling by creating blocks with syntax.

Input :

Output :

From the example above, why does the program output error occur?

Try to look carefully and with full accuracy,,,

In the first line there is a NameError in the word “I”, the word I is not in the parameter. So the program cannot recognize the word. So there is a NameError. It should be written “name” not “me”. But it can be solved in the following way:

The key is to put the word “try” before the command and the word “except (the name of the error) + what command will we run when the error occurs”. In the example I chose to have the command print “undefined sentence”.

If there are still other errors after we do exception handling then we find out what the error is other than that then the book of isa adds except again below it like

case study :

Make a formula to calculate the force ( F = m.a) and do exception handling if there is an input that is not a number.

Add a comment

Related posts:

TEORI KEPEMIMPINAN TERKINI

Kepemimpinan bukanlah hal “satu ukuran cocok untuk semua”. Seringkali, pemimpin harus menyesuaikan gaya mereka agar sesuai dengan situasi atau kelompok tertentu. Karnanya, penting untuk mendapatkan…