0

I have made a code which asks the users name and age. It asks age and stores it using :

name = input("")

And the name uses the same. I have outputted the age to file like so:

f = open('UserDetails')
f.write (name)
f.write (age)
f.close ()

I would like to be able to format this as in the file it just appears like:

James42Brian20Charlie56

I would like to make it be like :

James 42
Brian 20
Charlie 56

How would I do this?

5
  • Ok, let's do that: your question is lacking the code you have written, so we can apply the needed changes on it. You could also format the desired output as code, to make it more visible. Commented Apr 27, 2016 at 20:43
  • Literally my code is tiny. It asks the user to enter name and age, then it outputs to file. I did add the code to my question as text as I cannot figure out how to add it as code ( some features are not supported on ios) Commented Apr 27, 2016 at 20:45
  • @Gordon Adding as code is indenting by 4 spaces on a new line. you can also use backticks (shared with the tilde symbol on a qwerty keyboard) to do inline code. Commented Apr 27, 2016 at 20:48
  • Possible duplicate of writing string to a file on a new line everytime? Commented Apr 27, 2016 at 20:49
  • I deleted the comments about being downvoted, but if you would like to stop that, then your can read the various topics of asking help Commented Apr 27, 2016 at 20:50

5 Answers 5

2

Use string.format with a newline character like so:

f = open('UserDetails', 'a')
f.write("{0} {1}\n".format(name, age))
f.close()
Sign up to request clarification or add additional context in comments.

3 Comments

It is now saying that the f in f.close has incorrect syntax??
You should probably be opening the file with the 'a' flag so you can append your newly-written lines, or possibly with the 'w' flag if you want to overwrite previously-written ones.
What's the exact error you're getting, and what's the exact code you're using? You likely have a typo.
0
f = open('UserDetails','a')
name = input("Name? ")
f.write ("{} ".format(name))
age = input("Age? ")
f.write ("{}\n".format(age))
f.close ()

I would do something like this
You can put a while loop where needed
OR
You can run it multiple times if you wish to add another person

Comments

0

I haven't tested this, but did you try something like this?

separator = ' '
f = open('UserDetails') 
f.write (name + separator) 
f.write (str(age) + separator) 
f.close ()

It might not show exactly how you want it, but the idea is to use string concatenation and see if that works (including the new line symbol '\n'').

That said, without seeing the actual code you've written, it's hard to say much else.

1 Comment

You're correct. Using string.format would be the best option here, as shown in the other answers.
0

The with open as syntax is preferred over needing to manually close a file, and this will loop until you kill the script.

with open("UserDetails", "a") as f:
    while True:
        name = input("name: ")
        age = int(input("age: "))
        f.write("{0} {1}\n".format(name, age))

Comments

0
name = input("")
age = input("")
f = open('UserDetails','a')
f.write ("{0} {1}\n".format(name, age))
f.close()

4 Comments

This is python 2 syntax; the OP specified python 3
I tried this, and it has started claiming that the f in f.close has an invalid syntax ??
just remove the space after the word close.
While this code may answer the question, providing additional context regarding why and/or how it answers the question would significantly improve its long-term value. Please edit your answer to add some explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.