The link to my code on Github is this: https://github.com/Jocapear/TC1014/blob/master/WSQ16.py
]]> Writing a file:
To write a file you have to use the same function as before but this time with a different parameter: open(“filename”,”w”)
This “w” thing will thell python that you want to edit/write the file, be careful because this will erase the content of that file and create a new file. To write a line use fout.write() and use as a parameter a string (or a variable with a string), when you are done use fout.close(). It is very important that you understand that the file will star writing in the last empty line. Here is an example:
def process_file(filename):
name = open(“filename”,”w”)
a = “no”
while a == “yes”:
line = input(“Write here the new data”)
fout.write(line)
a = input(“want to close?”)
fout.close()
process_file(“file.txt”)
This function will add new strings to the “file.txt” document until the user decides to stop.
The FOR loop is used when you know the number of repetitions. It can be used with ranges, list tuples or dictionaries(iterable objects). It takes a value inside the object, work with it, and then do the same thing with the next value until the iterable object is finished.
The WHILE loop is used when you dont how the number of repetitons. It works with a condition and it will repeat until the condition is false.
Recursion is when you call a function inside the same function. It can be used in special cases where you need to take a value from the function itself, this job can be done with FOR or WHILE loops but it´s shorter to write a recursion.
]]>If you want to see my code i will post the Github link here:
https://github.com/Jocapear/TC1014/blob/master/WSQ14.py
]]>
If you want to see my code i will post it on Github, here is the link:
https://github.com/Jocapear/TC1014/blob/master/Quiz11.py