[Tutor] Hi there!
Alan Gauld
alan.gauld at yahoo.co.uk
Mon Nov 18 13:45:01 EST 2019
On 18/11/2019 17:37, Hamnah Baig wrote:
> Yes I'm unsure how to do all of these steps, could you please add some
> more detail?
>
> open input.txt and output.txt files
Read about file handling in my tutorial in the files topic
http://www.alan-g.me.uk/l2p2/tutfiles.htm
The basic method looks like this:
with open(filename) as alias:
use alias here
In your case you need two files opened so it will look like
with open("input.txt","w") as ilog:
with open(output.txt","w") as olog:
# your code goes here
> read input from user (or wherever it comes from)
You can read about user input in my tutorial here:
http://www.alan-g.me.uk/l2p2/tutinput.htm
Assuming you are reading input from a user you use the input()
function (raw_input in Python v2)
variable = input(prompt)
> write input to input.txt
The link above covers writing to files too.
You write to a file by converting the data to a string and
then using the files write() method
Something like:
ilog.write(str(mydata)+'\n') #add newline for file
> process input data
Only you know what you want to do here!
> write result to output.txt
This is the same as writing the input above except you write
to olog
> close input.txt and output.txt
The with statements automatically close the files so you
don't need to do anything extra.
Now you need to write some code and, if it doesn't work,
post it here so we can see where you are going wrong.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list