0

In class we are working on functions that calculate the area of a square or rectangle. The program asks for a person's name, what shape they want and what the length and width are. It then prints the area of that shape, and the program loops back around again. What I'm looking to do is take each individual name input and area and output them into a text file. Our teacher didn't make it too clear on how to do this. Any help would be appreciated. Here's the code:

import time

def area(l, w):
    area = l * w
    return area

def square():
    width = int(input("please enter the width of the square"))
    squareArea = area(width, width)
    return squareArea

def rectangle():
    width = int(input("please enter the width of the rectangle"))
    length = int(input("please enter the length of the rectangle"))
    rectangleArea = area(length, width)
    return rectangleArea

def main():
        name = input("please enter your name")
        shape = input("please enter s(square) or r(rectangle)")
        if shape == "r" or shape =="R":
            print ("area =", rectangle())
            main()
        elif shape == "s" or shape == "S":
            print ("area =", square())
            main()
        else:
            print ("please try again")
            main()  
main()

Edit: I don't think I asked the question clear enough, sorry. I want to be able to input something, e.g. the name and be able to put it into a text file.

2 Answers2

0

This is what you're looking for. The line file = open('file.txt', 'w') creates a variable file, in which the file object representing 'file.txt' is stored. The second argument, w, tells the function to open the file in "write mode", allowing you to edit its contents.. Once you have done this, you can simply use f.write('Bla\n') to write to the file. Of course, replace Bla with whatever you'd like to add, which can be your string variable. Note that this function doesn't make a newline afterwards by default, so you'll need to add a \n at the end if that's what you want.

IMPORTANT: As soon as you're done with a file, make sure to use file.close(). This will remove the file from memory. If you forget to do this, it won't be the end of the world, but it should always be done. Failure to do this is a common cause of high memory usage and memory leaks in beginner programs.

Hope this helps!

Edit: As MattDMo mentioned, it is best practice to open a file using a with statement.

with open("file.txt", 'w') as file: # Work with data

This will make absolutely sure that access to the file is isolated to this with statement. Thanks to MattDMo to for reminding me of this.

Henry Sloan
  • 59
  • 1
  • 6
  • Where am I able to see the file after? –  Jan 10 '17 at 21:48
  • You should always use the `with` context manager when dealing with file i/o. – MattDMo Jan 10 '17 at 21:52
  • `'file.txt'` will be created in the same directory as the script that is was created in. If you want it to be somewhere else, you can specify a file in a directory inside the one where the script is, or you can specify the entire path to any file on your computer. You can access this file like any other text file once it is created. – Henry Sloan Jan 10 '17 at 21:52
0

Easy way:

file_to_write = open('myfile', 'w') # open file with 'w' - write permissions
file_to_write.write('hi there\n')  # write text into the file
file_to_write.close()  # close file after you have put content in it

If you want to ensure that a file is closed after you finished all operations with it use next example:

with open('myfile.txt', 'w') as file_to_write:
    file_to_write.write("text")
taras
  • 3,579
  • 3
  • 26
  • 27