user1294395
user1294395

Reputation:

Printing multiple lines in a file?

Ok, well, i am trying to make a little program that asks you to enter a file name and it will create the file with the name you wanted. in that File it will display the adult price and the children price on two different lines. Anyone?

packagename = input("Enter package name:")
packagepriceadult = input ("Enter package price per adult:")
packagepricechild = input("Enter package price per child:")
a = open ("output.txt", "a")
a.write (packagename),"\n"
a.write (packagepriceadult)
a.write (packagepricechild)

Upvotes: 0

Views: 1096

Answers (3)

pythonian29033
pythonian29033

Reputation: 5207

if you're using python 2.x you can also do like I've done here:

outlist = [packagename, packagepriceadult, packagepricechild] a.write('\n'.join(outlist))

Upvotes: 0

yoniLavi
yoniLavi

Reputation: 2752

Another option available in python 2.x is to redirect the print statement to the file

print >>a, packagename
print >>a, packagepriceadult
print >>a, packagepricechild

See this post more info - slippens's blog

Upvotes: 1

Hamish
Hamish

Reputation: 23316

Simply write the linebreak to the file, for example:

a.write(packagename)
a.write("\n")
a.write(packagepriceadult)
a.write("\n")
a.write(packagepricechild)

Upvotes: 0

Related Questions