Login
Sign Up


You are using the mobile version of the forum, some features have been disabled to have it responsive.
Limelight Reunion 2024 - v4b1Limelight Discord
Ares Defence Services Discord
Limelight Reunion 2024 - v4b1Limelight DiscordAres Defence Services Discord

receiptDevelopment Blog:

Development Contributor Workflow

receiptHR Blog:

What *are* they doing over there?

receiptTeacher Blog:

Insight into the Teacher Team

receiptDevelopment Blog:

Infrastructure Upgrade 11/2019

receiptDevelopment Blog:

how suggestions???

receiptDevelopment Blog:

Planning for the future.


This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Can Anyone Help Me With GCSE Python Code ?
BENNAY radio_button_checked
User
Registered User
Posts: 5
Threads: 1
Likes Given: 1
Likes Recieved: 0 in 0 posts
Joined: Jan 2017
Reputation: 0
#1
Jul 5, 2017, 11:40 AM
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)
PaulB radio_button_checked
Developer
Veteran Member
Posts: 856
Threads: 62
Likes Given: 471
Likes Recieved: 519 in 277 posts
Joined: Aug 2015
Reputation: 14
#2
Jul 5, 2017, 12:31 PM
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
[Image: sigs.php?steamid=STEAM_0:0:81010017&t=2]
(This post was last modified: Jul 5, 2017, 12:44 PM by PaulB.)
Fly radio_button_checked
#LLSeriousRP
Membership
Posts: 465
Threads: 29
Likes Given: 66
Likes Recieved: 283 in 200 posts
Joined: Aug 2015
Reputation: 4
#3
Jul 6, 2017, 10:03 PM
(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 @PaulB. Is it necessary to exit the file object with its respective close() method, or is it just "bad practice" not to do so?
enjoy

++[------>+<]>-.[-->+++++<]>.+++++.+++++.++++[->+++<]>.+++++++++++++.++.[---->+<]>+++.-[--->++<]>-.+++++++++.---.-.-----.------.[--->+<]>-.+[->+++<]>.++++++++++++.--..--------.+++++++++.++++++.[---->+<]>+++.[->+++<]>+.+.+++++++++++++.++++++.-.[---->+<]>+++.--[->++++<]>+.----------.++++++.-[---->+<]>+++.--[->++++<]>-.-[->+++<]>-.--[--->+<]>--.+.-----------.+++++.-------.-[--->+<]>--.--[->++++<]>+.----------.++++++.---.[-->+++++<]>+++.---[->++++<]>.-----------.++++.--------.--[--->+<]>-.++[->+++<]>.+++.+++++.----------.+++++.+++++.-------.-[--->+<]>--.[->+++<]>+.-[->+++<]>.[->+++<]>++.[--->+<]>----.+++[->+++<]>++.++++++++.+++++.--------.-[--->+<]>--.+[->+++<]>+.++++++++.-[++>---<]>+.-[--->++<]>-.+++++.++++++.+++[->+++<]>.+++++++++++++.--.++.-------------.[--->+<]>---.+++[->+++<]>.+++++++++++++.[++>---<]>-.

(This post was last modified: Jul 6, 2017, 10:07 PM by Fly.)
PaulB radio_button_checked
Developer
Veteran Member
Posts: 856
Threads: 62
Likes Given: 471
Likes Recieved: 519 in 277 posts
Joined: Aug 2015
Reputation: 14
#4
Jul 7, 2017, 05:50 PM
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.
[Image: sigs.php?steamid=STEAM_0:0:81010017&t=2]
Fly radio_button_checked
#LLSeriousRP
Membership
Posts: 465
Threads: 29
Likes Given: 66
Likes Recieved: 283 in 200 posts
Joined: Aug 2015
Reputation: 4
#5
Jul 7, 2017, 09:49 PM
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.
enjoy

++[------>+<]>-.[-->+++++<]>.+++++.+++++.++++[->+++<]>.+++++++++++++.++.[---->+<]>+++.-[--->++<]>-.+++++++++.---.-.-----.------.[--->+<]>-.+[->+++<]>.++++++++++++.--..--------.+++++++++.++++++.[---->+<]>+++.[->+++<]>+.+.+++++++++++++.++++++.-.[---->+<]>+++.--[->++++<]>+.----------.++++++.-[---->+<]>+++.--[->++++<]>-.-[->+++<]>-.--[--->+<]>--.+.-----------.+++++.-------.-[--->+<]>--.--[->++++<]>+.----------.++++++.---.[-->+++++<]>+++.---[->++++<]>.-----------.++++.--------.--[--->+<]>-.++[->+++<]>.+++.+++++.----------.+++++.+++++.-------.-[--->+<]>--.[->+++<]>+.-[->+++<]>.[->+++<]>++.[--->+<]>----.+++[->+++<]>++.++++++++.+++++.--------.-[--->+<]>--.+[->+++<]>+.++++++++.-[++>---<]>+.-[--->++<]>-.+++++.++++++.+++[->+++<]>.+++++++++++++.--.++.-------------.[--->+<]>---.+++[->+++<]>.+++++++++++++.[++>---<]>-.

(This post was last modified: Jul 7, 2017, 10:12 PM by Fly.)
PaulB radio_button_checked
Developer
Veteran Member
Posts: 856
Threads: 62
Likes Given: 471
Likes Recieved: 519 in 277 posts
Joined: Aug 2015
Reputation: 14
#6
Jul 7, 2017, 11:55 PM
(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.
[Image: sigs.php?steamid=STEAM_0:0:81010017&t=2]
(This post was last modified: Jul 7, 2017, 11:57 PM by PaulB.)




Users browsing this thread: 1 Guest(s)