Python For GCSE
Section G Exercises
Remember to test your text files after each task to makes sure that you have the text you expected.
Write a program that creates a text file called task1.txt which contains only the text Hello World.
Write a program that creates a text file called task2.txt which contains the phrases Hello World and Goodbye World, written on separate lines.
Write a program that opens your task2.txt file in append mode. Write some text to the end of the file.
Copy the following.
h = "Hello" w = "World" g = "Goodbye"
Write a program that creates a file called task4.txt, containing the same content as the one you made for task 2, this time by using the contents of the variables.
Study the following,
a = open("a.txt","w") for i in range(1,13): a.write(str(i) + "x" + " some other stuff" + "\n") a.close()
It contains enough in the way of hints to allow you to create a program that outputs the 2 times table to a text file called task5.txt.
Use your experience in the previous task to output all of the times tables (2s to 12s) into a single text file called task6.txt.
names = ["Albert", "Bennet", "Charleston", "Denby", "Elfin", "Fishface", "Garbo", "Hubert", "Indigo", "Jelly"]
Write a program that outputs these names, one per line, to a text file called task7.txt.
The chr() function allows you to output a character from an ASCII code. The ASCII code for an A is 65. The rest of the alphabet follows. Write a program that outputs the alphabet to a text file called task8.txt, one letter per line.