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?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Gordon
  • 13
  • 1
  • 4
  • 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. – Walter_Ritzel Apr 27 '16 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) – Gordon Apr 27 '16 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`. – Robert H Apr 27 '16 at 20:48
  • Possible duplicate of [writing string to a file on a new line everytime?](http://stackoverflow.com/questions/2918362/writing-string-to-a-file-on-a-new-line-everytime) – Robert H Apr 27 '16 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](http://stackoverflow.com/help/asking) – OneCricketeer Apr 27 '16 at 20:50

5 Answers5

2

Use string.format with a newline character like so:

f = open('UserDetails', 'a')
f.write("{0} {1}\n".format(name, age))
f.close()
Hamms
  • 5,016
  • 21
  • 28
  • It is now saying that the f in f.close has incorrect syntax?? – Gordon Apr 27 '16 at 21:00
  • 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. – Hamms Apr 27 '16 at 21:02
  • What's the exact error you're getting, and what's the exact code you're using? You likely have a typo. – Hamms Apr 27 '16 at 21:02
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.

Fiery Phoenix
  • 1,156
  • 2
  • 16
  • 30
0
name = input("")
age = input("")
f = open('UserDetails','a')
f.write ("{0} {1}\n".format(name, age))
f.close()
Walter_Ritzel
  • 1,387
  • 1
  • 12
  • 16
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

taesu
  • 4,482
  • 4
  • 23
  • 41
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))
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245