Limelight Forums

Full Version: Can Anyone Help Me With GCSE Python Code ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need to save all of the user inputs to a text file
can someone edit the code if possible   and reply Big Grin


print("Use Yes Or No For Answers That Require It")
from datetime import datetime #imports time package
count=0 #sets count to 0
while True:
    count +=1
    if count >3:
        break
    name=input("what is your name\n")
    yob=int(input("what is your yob\n"))
    year = datetime.now().year #sets year
    age=year-yob # sets age as a constant
    print("so you are",age,)
    user_input=input ("is this correct\n")
    if user_input==("yes"):
        print("nice")
    else:
        print("oops, you must be",age-1)
Written on phone


with open("filename.txt", "a") as file:
     Data = all of your variables in a string
     file.writelines(Data)


edit: Remember to indent that ^ I Phone wont allow me to
(Jul 5, 2017, 12:31 PM)PaulB Wrote: [ -> ]Written on phone


with open("filename.txt", "a") as file:
     Data = all of your variables in a string
     file.writelines(Data)


edit: Remember to indent that ^ I Phone wont allow me to

I work a lot more with web backend, and had my fair share in Python, but i've rarely encountered the 'with' statement. Never used it myself.

To OP, remember to declare most global variables, imports etc. in the top. It's easier to navigate your code this way, and you won't accidentally get errors from "undefined" code elements.

EDIT: Just a question to . Is it necessary to exit the file object with its respective close() method, or is it just "bad practice" not to do so?
The reason I use with open is due to the fact it automatically closes when it isn't in use, I believe this is the quickest way in Python to open, read, write and close a file.
But I don't actually think the 'open' function actually closes while it isn't in use. I'd still write file.close(), after the while loop, to avoid it conflicting with other streamwriters.

This is without writing to the file.
Code:
>>> file = open("C:/PrivatePath", "a")
>>> file.closed
False

And this is with writing to it in a loop.
Code:
>>> for i in range(0,1):
    file = open("C:/PrivatePath", "a")
    file.writelines("ey")
//removed whitespace
>>> file.closed
False

So it doesn't automatically close apparently.
(Jul 7, 2017, 09:49 PM)Fly Wrote: [ -> ]But I don't actually think the 'open' function actually closes while it isn't in use. I'd still write file.close(), after the while loop, to avoid it conflicting with other streamwriters.

This is without writing to the file.
Code:
>>> file = open("C:/PrivatePath", "a")
>>> file.closed
False

And this is with writing to it in a loop.
Code:
>>> for i in range(0,1):
    file = open("C:/PrivatePath", "a")
    file.writelines("ey")
//removed whitespace
>>> file.closed
False

So it doesn't automatically close apparently.

Quote:The “with” statement invokes what Python calls a “context manager” on f. That is, it assigns f to be the new file instance, pointing to the contents of /etc/passwd. Within the block of code opened by “with”, our file is open, and can be read from freely.

However, once Python exits from the “with” block, the file is automatically closed. 
Edit: I only code python for my own GCSE course.