Reading and writing of text files

--Originally published at Py(t)hon

Computers can also read and write, for them to do so we have to first create a .txt file and we do this by typing file = open(“file name”, “action”).

The next thing that we can do are the next ones:

  • “w” this is use to write in a .txt file
  • “r” this is use to read
  • “a” open the file for apending

Here is an example:

txt1

That’s all #Pug#ISC#Tec#NoMore#Python#TextFiles


Validated user input (ensure correct/expected data entry)

--Originally published at Py(t)hon

Most of the time we want our program to be able to interact with the user, to ask him questions and him returning input, well, it is very important to validate the input the user is giving so our program doesn’t crash. For example we have a program were the user has to input a number, how we validate that that input will be indeed a number, we can use the try…except….else block, here is how:

valid-1

That’s all #Pug#Valid#Input#ISC#Tec#TC101#NoMore

 


Use of recursion for repetitive algorithms

--Originally published at Py(t)hon

This time we are going to learn about recursion, what is recursion? Recursion is a method where the solution to a problem is based on solving smaller instances of the same problem or in other words  is a way of programming or coding a problem, in which a function calls itself one or more times in its body.

An example is the function of the factorial:

fact-1

Click here for more examples.

Here is a video for better understanding:

#Pug#TC101#Tec#ISC#Recursion#NoMore