![]() |
Can Anyone Help Me With GCSE Python Code ? - Printable Version +- Limelight Forums (https://limelightgaming.net/forums) +-- Forum: Entertainment (https://limelightgaming.net/forums/forum-197.html) +--- Forum: Off-Topic (https://limelightgaming.net/forums/forum-208.html) +--- Thread: Can Anyone Help Me With GCSE Python Code ? (/thread-16075.html) |
Can Anyone Help Me With GCSE Python Code ? - BENNAY - Jul 5, 2017 I need to save all of the user inputs to a text file can someone edit the code if possible and reply ![]() 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) RE: Can Anyone Help Me With GCSE Python Code ? - PaulB - Jul 5, 2017 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 RE: Can Anyone Help Me With GCSE Python Code ? - Fly - Jul 6, 2017 (Jul 5, 2017, 12:31 PM)PaulB Wrote: Written on phone 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 @PaulB. Is it necessary to exit the file object with its respective close() method, or is it just "bad practice" not to do so? RE: Can Anyone Help Me With GCSE Python Code ? - PaulB - Jul 7, 2017 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. RE: Can Anyone Help Me With GCSE Python Code ? - Fly - Jul 7, 2017 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") And this is with writing to it in a loop. Code: >>> for i in range(0,1): So it doesn't automatically close apparently. RE: Can Anyone Help Me With GCSE Python Code ? - PaulB - Jul 7, 2017 (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. 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.Edit: I only code python for my own GCSE course. |